Skip to content
Katabench
Try free

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 trap

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().

Your job

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.

How it's graded

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.

SOLID principles exercises →

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