Java: The code becomes much cleaner with optional | By Oliver Foster | February 2025

My article is open to everyone; Non -member readers can click on this link to read the full text.
During programming, I encountered more traps than I had cups of milk tea, and memories linger.
Today, let’s dive into a subject that is both ordinary and deep – the art of zero verification, in particular the classic model != null
.
In the current battle with the infamous NullPointerexception (NPE), the “Null Pointer Nightmare” of the world of programming, this tip is almost the protective talisman of a programmer.
When I started to program, I was very sensitive to Null, almost like a radar.
I have been very careful with zero checks – if (object != null)
has become an opening line that I have constantly used.
But over time, as my experience grew up, I gradually realized that if the frequent checks were a safety net, they were also like adding a layer of bulky winter clothing to the code .
Not only did it make the code heavier, but it has also often led to bugs that are difficult to trace.
Imagine a code base filled with If-Else void checks in the shape of a labyrinth. It is not only difficult to find insects at a glance – no, even if you fold your eyes and use one …