jargon

Comparison

Null objectvsOption type

Null object

instead of returning null you returned something that implements the interface and does nothing, and the null checks disappeared.

Supplying a valid object with neutral behaviour in place of a null reference, so callers do not have to branch. It works beautifully for collaborators with no meaningful absence — a no-op logger, an empty collection, a permissive policy — and badly where absence really is a case the caller must handle, because it turns a loud failure into a silent nothing. That is the line between it and an option type, which makes absence explicit rather than invisible.

Full entry →

Option type

the type says the value might not be there, so you cannot use it without saying what happens when it is not.

A type with two cases — a value or nothing — making absence explicit and forcing the caller to handle it. It is the type-system answer to the null reference, and it differs from a null object in being visible rather than invisible: an option makes the caller decide, a null object silently does nothing. Its cost is ceremony in a language without pattern matching or a decent option API.

Full entry →

Related comparisons