Faster StringBuffer.append()

09 Dec 2005

Allan Caplan has implemented another fine PMD rule - AppendCharacterWithChar. This rule catches code like this:

void bar() {
 StringBuffer foo = new StringBuffer();
 foo.append("x");  // Just use foo.append('x')!
}

It's a small change, but if you look at the code in the various overloaded versions of StringBuffer.append() you'll see how much less work is done when appending a single character vs appending a String object. This rule had a number of hits in the JDK - ChoiceFormat.toPattern(), Timestamp.toString(), URLStreamHandler.toExternalForm(), java.util.regex.Pattern.normalize(), and various others.

For the adventurous, I've uploaded a newer version of pmd-3.4.jar that contains this new rule (in the 'strings' ruleset).