Sometimes I face I must write a piece of code like this (usually it have more nested if and more complex structure but for the example is enought)
public void printIt(Object1 a){
if (a!=null){
SubObject b= a.getB();
if (b!=null){
SubObject2 c=b.getC();
if(c!=null){
c.print();
}
}
}
}
when I dont need to know what failed and if something is null do nothing, an approach could be
public void printIt(Object1 a){
try{
a.getB().getC().print();
}catch (NullPointerException e) {
}
}
Is there something wrong in this second form like performance or other kind of issues?
The exception version (similar to chains using Groovy’s safe-navigation operator
?.) makes it really easy to take the Law of Demeter (or as I call it, Demeter’s Strongly-Worded Suggestion) and make it your plaything for the night.Similarly, deeply-nested
if-statements leads to difficult-to-read code, and underneath it all, the same “violation” exists, and the cyclomatic complexity of such methods is high.I’d rather see LAMs (Little Auxiliary Methods) in appropriate places that encapsulate the checks and pretty much eliminate the need for the question altogether.