Optional.isEmpty()

Peter Levart peter.levart at gmail.com
Sat Apr 22 09:40:24 UTC 2017


Hi,

Seeing the following line in some JDK test that was up for review:

     return cf.findModule(target).orElse(null) == null;

I immediately jumped to suggest it would look better if written as:

     return !cf.findModule(target).isPresent();


But then I leaned back and asked myself: "Would it really?"

The boolean negation in front of a chain of method calls tends to 
visually disappear from the view when we finally reach the offending 
method and might get missed when quickly browsing the code. In this 
respect, ".orElse(null) == null" is actually better. But the best would 
of course be something like that:

     return cf.findModule(target).isEmpty();

What do you think? Would this pull its weight?

Regards, Peter



More information about the core-libs-dev mailing list