Experienced Developers Use These 7 Java Optional Tips to Remove Code Clutter
Here are the 7 most common Optional cases of abuse I encounter
“I have coworkers that don’t know Optional exists.” — one Java developer on Reddit.
Do you see Optional
“readability” improvements? Do you need to constantly explain Optional
usage? Do you see Optional
abuse?
Most developers still don’t know what’s Optional
correct usage. Some don’t even know it exists.
Here are the 7 examples of Optional
abuse and what you can do about them.
1. Don’t use Optional
for null checks
Developers use Optional instead of a null check. Optional only adds overhead. And there’s no benefit to doing this.
An Optional
mistake was to add isPresent
and get
method. That's what even Brian says is a mistake. Developers think get
must work as expected. Then they add isPresent
and you're back to null checks again.
Developers don’t understand why Optional
exists.
Optional
doesn’t fix your null checks. Optional
should represent the absence of value.
They’re keen to use Optional
whenever possible.