Wednesday, August 5, 2009

Things I Learned From My First Code Review

Here's some interesting ideas I saw in my first code review at OCLC

instead of setting to a non-null in constructor to prevent null from being returned, check before it is returned by
doing this
public String getSomeString() {
  return (headerTag == null) ? new String() : headerTag;
instead of this
String foo = new String();
public String getSomeString() {
  return foo;

for String concantenation, StringBuffer is faster than String ( Unlike Strings, which are immutable, StringBuffers are mutable.

surround your log4j debug log statements in an
if (log.isDebugEnabled()) {
  log.debug("message here")
so that the string isn't needlessly constructed (

avoid NPEs in comparisons, do:
String foo = "Some_String";
if (foo.equals(someString)) { ... }
instead of:
if (someString.equals("Some_String")) { ... }