Pages

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 (http://java.sun.com/developer/JDCTechTips/2002/tt0305.html). 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 (http://logging.apache.org/log4j1.2/faq.html#2.3)

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

No comments:

Post a Comment