Null Object: No Branching
Medium About 20 min Free, no account needed
A cart guards a nullable discount with a null check at every use. Introduce a NoDiscount Null Object so the cart never branches on absence, but do not let the cart hard-code that default. The obvious 'default it inside the cart' move re-couples the cart to a concrete; the default belongs to the composition root.
The brief
You've inherited the checkout cart. Checkout.Cart holds a discount that may or may not be set, so
Total guards its absence with a null check: if (_discount == null) return amount;. There's one
branch today, but the shape invites another at every new call site, and "did I remember the null
check?" becomes a standing tax. The IDiscount abstraction already exists. What's missing is a real
object to stand in for "no discount", so the branch has nothing left to guard. That's the Null Object
pattern: replace null with a do-nothing implementation of the same contract, and the consumer stops
branching on absence.
The tempting move is the obvious one. Write a NoDiscount that applies zero, then make the cart hand
itself that default: _discount ??= new NoDiscount() in the constructor, or (_discount ?? new NoDiscount()).Apply(amount) at the use. Seal the cart while you're there. Do exactly that and the
"a NoDiscount exists" and "Cart is sealed" rules go green, but one rule stays stubbornly red, and
it's the one that matters: the cart now names the concrete NoDiscount. You set out to remove a
branch and instead welded the cart to a specific discount. The null check is gone; the coupling that
made it brittle is not. It just moved from a == null into a new NoDiscount().
Make absence a real, injected value. Keep the cart depending only on IDiscount, always holding one,
and let the composition root (the one place allowed to know concrete types) decide that "no discount
requested" means a NoDiscount. The behavior must stay identical: same total for the same inputs.
Your submission must (1) still pass the behavior tests (identical totals) and (2) satisfy
every rule shown in the results, including the one pinning that Cart references no concrete
NoDiscount at all, not in a field default and not in a fallback expression. The naive "default it
inside the cart" move clears the easy rules and leaves that one failing; only a cart that takes its
discount from the outside, with the composition root supplying NoDiscount as the default, turns them
all green.
A Null Object removes the branch. It does not give you license to bake the default into the consumer. Who holds the default is a wiring decision, and wiring lives at the composition root, not inside the class that's trying to stay ignorant of which discount it has.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Null object pattern
- Replace repeated null checks with a do-nothing implementation.
- Dependency injection
- Ask for collaborators through constructors instead of newing them up or locating them.
Related katas
- Retire the Service Locator Medium Free A quote calculator fetches its rate table from a global ServiceLocator, so its one real dependency is invisible in every signature. Replace the locator with constructor injection. Caching the located table into a readonly field keeps the calculator quietly coupled to the global and leaves the key rule red.
- Ask for the Interface Easy Free An order notifier news up its own SmtpEmailer and types the field as the concrete class, even though an IMessenger abstraction sits right next to it. Make the notifier ask for the interface in its constructor. Typing the field as IMessenger while still newing the concrete inside is not the fix.
- Compose the Fees Hard Free Account fee variation is modeled by subclassing: PremiumAccount and StudentAccount each override MonthlyFee, so the base can never be sealed and every new flavor is a new class. Flatten the hierarchy into one sealed Account composed with an injected IFeePolicy. Extracting the policy but hard-coding a default inside the account trips the composition-root rule.
- Fakes Don't Ship Easy Pro A test stub lives in the core and the checkout quietly falls back to it when nobody injects a gateway. Evict the stub to the testing namespace and make the dependency mandatory; a convenient default is a production code path.