DEV Community

Discussion on: Use Optional / Nullable Reference Types Judiciously

Collapse
 
bob profile image
Bob

Yes. Optional will not just magically eradicate all our NPEs. But it is possible to write better code using Optional. As I said in the above post itself,

"The advantage compared to the normal null references is that the Optional types forces you — or the client of your API — to think about the case when the value is not present."

So now the client of the APIs should check whether the Optional has value or not before using it. For a non-optional variable, they can assume it will never be null. For Kotlin the system itself won't allow null reference to a non-optional. But in Java, we have to ensure we don't mistakenly allow a null reference to a non-optional field.