![Cover image for How to Make Your Reviewer Cry Using Java Optional](https://media.dev.to/cdn-cgi/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foy9nbk4r6on5ay29115d.jpg)
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...
For further actions, you may consider blocking this person and/or reporting abuse
This line could be made simpler:
is equivalent to
Not like you described, but like this
With method reference.
If the method was not static and was an instance one, then it would be like this
Hi Joshua. I don't think so; It faces a compile error.
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.
Actually, this issue is related to all of the lambda expressions. Take a look at this article:
baeldung.com/java-lambda-exceptions
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.
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.
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.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.
Hmmm... it was really interesting! thank you bro ;)
Thanks buddy. So happy to hear that :)
Thank you Majid. It was very good.
You're welcome, dude :)
Thank you !
You're welcome, dude ;)
Great article - I applaud java for introducing Optional but always wish it was different. I tend to go for Vavr these days.