What is the best null alternative in object oriented programming?

I don't feel satisfied with avoiding null in OOP. Is there an alternative solution? I do not like to avoid this this way .

What is the best way to handle this?

+6
source share
3 answers

Java 8 has a new Optional type, which I think you are asking about

A container object that may or may not contain a nonzero value. If a value is present, isPresent() will return true and get() will return a value.

+10
source

Although you don't like it, Null Object Pattern is one of the best ways to avoid a null value. Also never return null when you can return an empty list.

+2
source

I want to add a property of the static class Empty. This returns a specific value for an object that can be compared to - like string.Empty in .NET

+1
source

Source: https://habr.com/ru/post/979381/


All Articles