Thursday, December 30, 2004

Findbugs

This morning I got FindBugs working; for some reason when I tried it some months ago I had problems.

So far it's been helpful, finding a number of things that I either wasn't looking for or had overlooked.

One mistake, which I've reported, was when it said that you should use StringBuilder instead of String when building complex Strings. It turns out that the Java compiler is fairly clever when compiling Strings; it converts them to StringBuilder objects.

Try it. Compile this (based on the code that FindBugs presented as 'bad'):

public class Test {

Integer[] field = new Integer[10];
public String toString() {
String s = "";
for (int i = 0; i < field.length; ++i) {
s = s + field[i];
}
return s;
}
}
Then run
javap -verbose Test
See all the StringBuilder operations?

(Later: see the first comment. Apparently the compiler only does a credible job on very simple cases).