I don't feel satisfied by avoiding null in OOP. Is there any alternative solution? I don't like to avoid it in this way either.
What is the best possible way to handle it?
I don't feel satisfied by avoiding null in OOP. Is there any alternative solution? I don't like to avoid it in this way either.
What is the best possible way to handle it?
Java 8 has the new Optional
type which is what I think you are asking about
A container object which may or may not contain a non-null value. If a value is present,
isPresent()
will return true andget()
will return the value.
Although you don't like it, the Null Object Pattern is one of the best ways to avoid null. Also never return null when you could return an empty list.
I favour adding a static class property of Empty. This returns a specific value for the object which can be compared against - like string.Empty in .NET