Friday, December 8, 2006

Firebug

Firebug is an incredibly useful Firefox extension for developers. You can debug JavaScript, edit HTML and CSS, inspect the DOM, determine the download times of all of the elements on a page, inspect headers, and much more.

Firebug 1.0 beta has just been released. I can't say enough good things about this extension. If you are a Web developer, it really should be part of your tool set.

Tuesday, December 5, 2006

Java's Enhanced For Loop Mystery

I like Java 5's new enhanced for loop. It makes the code that iterates over collections and arrays less verbose. Anything that reduces Java's verbosity is a good thing in my book.

Yesterday I wondered if the Java 5 enhanced for loop calls the collection expression once or every time through the loop. I searched for the answer online, and couldn't find one. Every example I found assigned the collection or array to a variable first and then used that variable in the expression, like this:

  String[] list = {"a", "b", "c"};
  for (String item : list) {
    // ...
  }

What I was looking for was a statement like, "the list expression is only evaluated once". After a few minutes, I realized that it would be faster to write a small program to determine the answer. (See Just Try It.)

import java.util.*;

public class Test {

  public Collection<String> stringCollection() {
    System.out.println("I'm creating a new list now.");
    // Don't get me started on the verbosity of the next line...
    ArrayList<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    return list;
  }

  public static void main(String[] args) {
    Test t = new Test();
    for (String s : t.stringCollection())
      System.out.println(s);
  }
}

With a simple javac Test.java && java Test I had the answer: stringCollection() is only called once.

Just Try It

I love answering programming questions. It lets me feel like a grizzled veteran sharing my arcane knowledge with those eager to learn. (Yes, I'm delusional.) If I don't know the answer, I enjoy finding out.

Most people don't know all the arcane language rules of every language they use. As I get older, I'm less inclined to memorize a language spec or an API. For example, yesterday I wondered if the Java 5 enhanced for loop calls the collection expression once or every time through the loop. I searched for the answer online, and couldn't find one. After a few minutes, I realized that it would be faster to write a small program to determine the answer. Within two minutes, I knew. (I'm saving the answer and the code for another blog post.)

If you have a question about a language rule or the use of a library, there is more than one way to find the answer: read the documentation, ask somebody else, or try it. If at all possible, Just Try It™—write a small program that answers your question. Even better, if your language has a REPL or even accepts input from stdin, then type a few lines of code interactively.

Ruby comes with irb, the Interactive Ruby Browser. Every Lisp has a REPL. Smalltalk has the Workspace. Shell languages have the shell.

When you Just Try It, you gain the immediate satisfaction of knowing the answer. You also use a different part of your brain then when you read the answer online; I claim you absorb the answer more thoroughly if you write a small code snippet. You might want to keep a collection of these code snippets for yourself. I have a personal Wiki (an Emacs Wiki, of course) where I store these sorts of things if they're useful enough. There are also a few snippet collection Web sites out there, such as Code Snippets.

Settling In

Welcome to the new home of Shiny Things, a blog about programming, programming languages, tools, frameworks, programming productivity tips, and whatever else catches my eye.

The old home of Shiny Things will remain. One reason I moved Shiny Things here is to allow comments. I haven't decided if I will copy some or all of the posts.