Null check
January 11th, 2006
Java developers are trained time and again to assert that objects are not null. This can result in weird Pavlov-like reflexes. Witness this snippet from a high-profile OSS project:
if (defaultValue != null) {
return defaultValue;
} else {
return null;
}

April 21st, 2008 at 10:18 PM It could be worse... return defaultValue!=null?defaultValue:null; :)
April 21st, 2008 at 10:18 PM why not? return defaultValue;
April 21st, 2008 at 10:18 PM Why not name the offending project? I'd like to know where you saw this.
April 21st, 2008 at 10:18 PM This is MUCH better: try{ String str = defaultValue.toString(); } catch(NullPointerException ex){ return null; } return defaultValue;