Thursday, December 17, 2009

Runtime exceptions: the good, the bad and the ugly #1

Reading others' source code is useful, but sometimes very annoying. Programmers often code routinely and this may lead to subtle but evil mistakes. Handling null references is a good example. I often the following piece of code:
if ("foo".equals(bar)) {
This kind of expression can be useful in cases when bar is allowed to be null, and we don't want to write an explicit check for it. But it should not be used when bar should not be null, because it eliminates a NullPointerException and no one will know about the error until the first failure.