5

We have all seen that kind of code

if (myObject!= null 
  && myObject.mySubObject() != null
  && myObject.mySubObject().getSpecificValue() != null
  && !myObject.mySubObject().getSpecificValue().isEmpty()
) {
     ......
}

How could I write this the clean way ?

10
  • 4
    Unfortunately, this is the "clean way". Welcome to Java! Commented Jan 29, 2020 at 15:58
  • 2
    Optionals are an alternative, but they need training to handle right, otherwise they are as messy as this. However, as @Jordan already noticed, this kinda is the way you do it in java... Commented Jan 29, 2020 at 15:59
  • 1
    Does this answer your question? Java "?" Operator for checking null - What is it? (Not Ternary!) Commented Jan 29, 2020 at 16:00
  • 2
    Does this answer your question? stackoverflow.com/questions/27074474/… Commented Jan 29, 2020 at 16:04
  • 1
    Agreed with @Jordan, also NPE can come handy in cases where the target object are too deep level. just Use try catch block for NullPointerException If in catch, treat as else, otherwise, proceed using the target object in try block as if block. It's not worth mentioning this as answer, so putting in comments only try { Object o = myObject.mySubObject().getSpecificValue(); // use o further } catch(NullPointerException npe){ // else logic can come here; } Commented Jan 29, 2020 at 16:06

2 Answers 2

6

You can do chaining with Optional:

Optional.ofNullable(myObject)
  .map(o -> o.mySubObject())
  .map(so -> so.getSpecificValue())
  .map(sv -> sv.isEmpty())
  .orElse(false)

Or with method references even shorter (does the same):

Optional.ofNullable(myObject)
  .map(Foo::mySubObject)
  .map(Bar::getSpecificValue)
  .map(Baz::isEmpty)
  .orElse(false)

where Foo, Bar and Baz are the names of the respective classes.

Sign up to request clarification or add additional context in comments.

11 Comments

Neat way of using Optionals!
You can also use method references.
@miiiii No, because Optional#map will only continue if the Optional is present, i.e. not empty. So this is ensured by starting with Optional.ofNullable. That is the whole point of the map method in Optional.
@Zabuza just wrote a code in IDE and ran as I was not convinced yet. But You're right. It works as expected. I wasn't aware of this. Thanks. I wish could give upvote for your explanation. :)
Yup. Agreed. I must say, the way Java has implemented functional programming paradigm is much nicer than traditional Functional Languages I've explored so far. Nice to talk to you. :)
|
1

If you are using someone else's code then you're really stuck handling for a possible null. On the other hand, if you have control over the code base then never return a null object, and make that a rule across your entire application.

This may sound bad at first but I have developed several enterprise-level applications and this is a very effective way to make the code consistent and much more readable.

So, now, this

if (myString != null && !myString.isEmpty()) {

becomes simply

if (!myString.isEmpty()) {

In lue of that option use the new Optional feature in J8 as it is intended for that purpose.

1 Comment

This is the correct answer: Do not treat null as a normal value. Most of the time, null is not needed in complex objects, especially collection properties.

Your Answer

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.