3

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

Buhake Sindi
89.4k30 gold badges176 silver badges234 bronze badges
asked Feb 27, 2015 at 12:22
7
  • 2
    Look here: oracle.com/technetwork/articles/java/… It is exactly explained your problem Commented Feb 27, 2015 at 12:25
  • I would say that your architecture/design is not right if you have such problem. As you said yourself, you violate Law of Demeter, which could solve this problem. Could you provide a broader context of what you are trying to do? Commented Feb 27, 2015 at 12:25
  • @NwDx Optional won't help here, it would be a set of nested isPresent() calls. Commented Feb 27, 2015 at 12:25
  • @JaroslawPawlak: Nope, you can call the latest get and only test there. Commented Feb 27, 2015 at 12:26
  • Even i agree this is very bad design very tight couple. This API i am consuming so cant tell them to provide isPresent() or isEmpty() method on Parent .I dont have any other option. Commented Feb 27, 2015 at 12:53

2 Answers 2

2

Null Object design pattern is the way to which is being absorbed in Java 8 via Optional class which means you have a wrapper within which either you have the data or you have empty data.

Its something like

 MyObject
 RealObject NullObject

Where instead of passing null, you pass NullObject which provides the same interface as MyObject (which can be concrete/abstract/interface class)

answered Feb 27, 2015 at 12:27
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @SMA for you input i have checked that before posting here but that is for Java8 right?But i am looking for Java 7
Here I am consuming API so i cant tell them to pass empty nested object.
Try utilising the same concept as i stated Null Object design pattern. See the class hierarchy that it follows (i just gave a hint above in my answer)
@user2387280 Your project should be isolated from code that you cannot change. Create your own isolation layer which will expose only the necessary part of third party API you are using. If you are using TDD, this should have integration test.
Do u see any problem in my option 1?I am handlingexception
|
2

This needs Java 8, you are right. I think this will function in a similar way in Guava.

public class OptionalTest {
 public static void main(String[] args) {
 A a = new A();
 Optional<A> opa = Optional.ofNullable(a);
 int accid = opa.map(A::getClassB).map(A.B::getClassC).map(A.B.C::getClassD).map(A.B.C.D::getAccountID).orElse(-1);
 if (accid > -1) {
 System.out.println("The account id is: " + accid);
 } else {
 System.out.println("One of them was null. Please play with commenting.");
 }
 }
 static class A {
 B b = new B();
 //B b = null;
 B getClassB() {
 return b;
 }
 static class B {
 //C c = new C();
 C c = null;
 C getClassC() {
 return c;
 }
 static class C {
 D d = new D();
 //D d = null;
 D getClassD() {
 return d;
 }
 static class D {
 private final int accountId = 2;
 int getAccountID() {
 return accountId;
 }
 }
 }
 }
 }
}
answered Mar 1, 2015 at 9:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.