Best practice with respect to NPE and multiple expressions on single line

Posted by JRL on Stack Overflow See other posts from Stack Overflow or by JRL
Published on 2010-06-03T14:16:40Z Indexed on 2010/06/03 14:24 UTC
Read the original article Hit count: 279

I'm wondering if it is an accepted practice or not to avoid multiple calls on the same line with respect to possible NPEs, and if so in what circumstances. For example:

getThis().doThat();

vs

Object o = getThis();
o.doThat();

The latter is more verbose, but if there is an NPE, you immediately know what is null. However, it also requires creating a name for the variable and more import statements.

So my questions around this are:

  • Is this problem something worth designing around? Is it better to go for the first or second possibility?
  • Is the creation of a variable name something that would have an effect performance-wise?
  • Is there a proposal to change the exception message to be able to determine what object is null in future versions of Java ?

© Stack Overflow or respective owner

Related posts about java

Related posts about best-practices