Professional Documents
Culture Documents
You have 2 free stories left this month. Sign up and get an extra one for free.
. . .
Optional Lessons
Consider the Optional class, introduced in Java 8. By now, you’ve
probably heard the mantra that you should never return null from a
method that returns an Optional . Why? Consider the following contrived
example:
Now clients can use the method like this and risk throwing a
NullPointerException :
foo.getBar().ifPresent(log::info);
if (foo.getBar() != null) {
foo.getBar().ifPresent(log::info);
}
Of course, we’d never write a method like that. Doing so defeats the very
purpose of Optional s. In fact, it so defeats the purpose of Optional s that
it’s nothing short of a golden rule that any API that returns Optional will
never return a null value.
The drawer still exists even when all the socks are removed, right?
It’s for this reason that it’s becoming more common to ensure that
methods that return Collection types (including arrays) never return
null values, the same as methods that return Optional types. Perhaps you
or your organization have already adopted this rule in writing new code.
If not, you should. After all, would you (or your clients) rather do this?:
. . .
method. In fact, a fresh instance would do just that. So, to adhere to our
new never-return-a-null-Collection guideline, we need to fix that:
Now, even when null is passed to the setter, myStrings will retain a valid
reference. So are we all done?
Sort of. We could stop here. But really, there’s more we should do.
Consider that we are still replacing our private ArrayList with the List
myClass.setMyStrings(Collections.unmodifiableList("Heh, gotcha!"));
underlying List . So now, that caller can now directly manipulate our
List .
method:
This has the effect of ensuring that myStrings ends up with the same
elements contained within the input parameter (or is empty if null is
passed) while ensuring that the caller doesn’t have a reference to
myStrings .
While we’re at it, we shouldn’t be returning our underlying List via our
getter. That too would leave the caller with a direct reference to
myStrings . To remedy this, recall the “defensive copy” mantra that
Effective Java beat into our heads (or should have):
And this:
And so on:
always does just that — returns a Collection — and never returns null .
WRIT T EN BY
Dave Taubler Follow
Enough Vim to Be Create a Data Marvel : Outcome of Our First I Wrote a Python Web
Dangerous Develop a Full-Stack Call for Contributors for Scraper T hat Sends Me
Patrick Divine in Better Application with Spring DeckDeckGo Text Messages About
Programming and Neo4j—Part 1 David Dal Busco in Better Job Postings
Jennifer Reif in Neo4j Programming Zack Claar in Better
Developer Blog Programming
Learn Other Skills to Snippets About Exceptional Exceptions Why You Should Learn
Transform Your Code Concurrency for Coroutines made Flutter
Chris Cooney in Better Nicolas A Perez in easy…? Faisal Choura in Better
Programming HackerNoon.com Anton Spaans in T he Kotlin Programming
Chronicle