So, what did the VP of TPM do wrong? Additionally, what did the code reviewer
and developer do wrong? Well, the answers are at the end of this article, but
if you didn't intuit that the code that will go into production is still open
for NPE, then maybe this article is for you. And, yes, somehow the code
passed the testing phase as well.
The best way to use things is to exploit them for what they have been created
and tested for in the first place. Java 8 Optionalis not an exception to this
rule. The purpose of Java 8Optionalis clearly defined by Brian Goetz, Java’s
language architect:
Optional is intended to provide a limited mechanism for library method return
types where there needed to be a clear way to represent “no result," and
using null for such was overwhelmingly likely to cause errors.
So, how to useOptionalthe way it was intended? Typically, we learn to use
things by learning how to not use them, and, somehow, this is the approach
here as well. So, let's tackle this topic via 26 items. This is a suite of
items that try to addressOptionalin your code through an elegant and painless
approach.
If you like this article then you'll love my book, Java Coding Problems
https://dzone.com/articles/using-optional-correctly-is-not-optional#:~:text=The%20purpose%20of%20Java%208,overwhelmingly%20likely%20to%20cause%20errors.