I want to retrieve the field from API which present inside classes. Yes I know this is against Law of Demeter but I don't have any option.
Example
getClassA().getClassB().getClassC().getClassD().getAccountId();
So to add null check as its bad code smell so I come with below code:
try{
getClassA().getClassB().getClassC().getClassD().getAccountId();
}catch(NullPointerException ex){
S.O.P("Null Found");
}
or
ClassA a = getClassA();
if(a!=null){
ClassB b = a.getClassB();
So on.....
}
my question is which is best approach the above mentioned one or explicitly retrieve each class and check null and go to next level This is against Law of Demeter
isPresent()calls.