Re:Java Best Practices – String performance and Exact String...
by pittsburgh on Thursday July 14, 2011 @02:05PM
(#2764200)
Along the lines of String best practices, my favorite is to reverse the usual order of variable and literal to avoid null pointer exceptions:
instead of:
[ Parent
| on HN ]
Re:Java Best Practices – String performance and Exact String...
by Sandman on Thursday July 14, 2011 @04:21PM
(#2764900)
Yes, you avoid NPE, but consider this piece of code:
//fetch the name of bacteria from somewhere
String bacteria = getBacteriaName();
if("botulinum".equals(bacteria)) {
//do something
} else {
//do something else
}
Now, if for any reason the getBacteriaName() method failed and gave you a null instead of some string, the code will end up doing whatever it is you want it to do if the bacteria name is other than "botulinum". But this might not be what is really desired. The method failed and this should be taken into consideration. The string bacteria doesn't contain the name of some other bacteria, it's null. The problem here is that, under the circumstances, perhaps the code should have returned 'botulinum' but since the method failed, the program acts as if another bacteria name was fetched instead of throwing an exception. Just something to be careful about.
Re:Java Best Practices – String performance and Exact String...
by tcopeland on Thursday July 14, 2011 @02:15PM
(#2764255)
Folks might also be interested in the PMD string checks:
Re:Java Best Practices – String performance and Exact String...
by kodablah on Thursday July 14, 2011 @02:20PM
(#2764280)
And checkstyle: http://checkstyle.sourceforge.net/config_coding.html#EqualsA...