DEV Community

Cover image for How to Make Your Reviewer Cry Using Java Optional

How to Make Your Reviewer Cry Using Java Optional

Majid Abarghooei on December 25, 2020

I think code review is one of the best sources of inspiration. I see it as an opportunity to learn new things from other software developers sendin...
Collapse
 
joshuakb2 profile image
Joshua Baker

This line could be made simpler:

System.out.println("The number is: " + number.orElseGet(() -> alternativeNumber()));
Enter fullscreen mode Exit fullscreen mode

is equivalent to

System.out.println("The number is: " + number.orElseGet(alternativeNumber));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
thegoncalomartins profile image
Gonçalo Martins • Edited

Not like you described, but like this

System.out.println("The number is: " + number.orElseGet(NameOfTheClass::alternativeNumber));
Enter fullscreen mode Exit fullscreen mode

With method reference.

If the method was not static and was an instance one, then it would be like this

System.out.println("The number is: " + number.orElseGet(this::alternativeNumber));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dante0747 profile image
Majid Abarghooei

Hi Joshua. I don't think so; It faces a compile error.

Collapse
 
clavinjune profile image
Clavin June

Java Optional is a life-safer! Even I created a blog post for it too. But still its map function can't call another function that throws an exception except you catch it.

Collapse
 
dante0747 profile image
Majid Abarghooei

Actually, this issue is related to all of the lambda expressions. Take a look at this article:
baeldung.com/java-lambda-exceptions

Collapse
 
denday04 profile image
Andreas Stensig

Good article. Though I will add a legitimate use for orElse(null) that I encounter a lot: when transitioning from code that operates on Optional types to code that does not (and which accepts null refences). Sadly this is something that happens a lot when you try and use Optional, because a lot of frameworks cannot handle that gracefully - sad and infuriating as it is.

Collapse
 
dante0747 profile image
Majid Abarghooei

Yes, Andreas, you're right. Unfortunately, accepting null references is a bad practice that I usually face. In that case, especially when you're dealing with a framework, I think it's inevitable.

Collapse
 
aaiezza profile image
Alessandro Aiezza

This is good, but I would have loved to see another common pitfall I see a lot of. And that is accepting an Optional as a constructor or method parameter.

Collapse
 
dante0747 profile image
Majid Abarghooei

Thanks for your comment. Exactly, you're right. I see it a lot. I tried to cover that in the first section ("What is Optional?"), where I referred to Optional's Javadoc.

Collapse
 
matin profile image
Matin

Hmmm... it was really interesting! thank you bro ;)

Collapse
 
dante0747 profile image
Majid Abarghooei

Thanks buddy. So happy to hear that :)

Collapse
 
darvishm profile image
Mohammed

Thank you Majid. It was very good.

Collapse
 
dante0747 profile image
Majid Abarghooei

You're welcome, dude :)

Collapse
 
rodrigovvo profile image
Rodrigo Vinícius

Thank you !

Collapse
 
dante0747 profile image
Majid Abarghooei

You're welcome, dude ;)

Collapse
 
_bkern profile image
Barry

Great article - I applaud java for introducing Optional but always wish it was different. I tend to go for Vavr these days.