25

Introduce Special Case


Goal

A repeating null-or-special check becomes a Null Object (or Special Case) that responds sensibly to the same interface.

Before the refactoring
const name = customer === 'unknown' ? 'occupant' : customer.name;
After the refactoring
const name = customer.name; // UnknownCustomer.name returns 'occupant'
Savings

Callers stop branching on identity; the special behavior lives in one place.

Note

Adds a tiny class for one case; only worthwhile when the special case appears in 2+ consumers.