Kill the Global Counter
Medium About 15 min Free, no account needed
Counter keeps its running total in a public static field every caller shares, and it's an open, inheritable class on top. Give the state one explicit owner instead of just hiding the field, and don't let the reflex private static fix trick you into leaving the second smell standing.
The brief
You've inherited a Counter. It's supposed to count a sequence: call Next() a few
times, read the running total. It does, but it does it with ambient mutable static
state.
The total lives in public static int Value, and Next() bumps that one field. Run a
single sequence in isolation and the numbers look right, so the behavior tests pass.
But that cell is process-global: two unrelated callers stomp on each other's count,
the result depends on whatever ran before, and any code anywhere can reach in and write
Counter.Value = 99. The composition root only gets a clean count because it remembers
to call Reset() first. The isolation is ceremony, not structure.
On top of that, Counter is an open, inheritable class. Because the state is static,
a subclass can't give it a private owner; it just inherits the same global cell and can
hang more shared state off the back of it.
The reflex fix is to make the field private static and surface the total through a
public static accessor. That looks tidier, and it does silence the "no public
mutable static state" complaint, because there's no public mutable static field left.
But it doesn't make the state any less global: it's still one shared cell for the whole
process, two callers still collide, and you still have to Reset() between runs. You've
renamed the smell, not removed it. And it does nothing about the second problem at all.
Counter is still an unsealed class, so one of the rules has nothing to do with the
field and stays red no matter how you hide it.
Hiding the field clears one symptom and leaves the others standing.
Give the state a single, explicit owner so Counter passes every rule, not just
the easy one:
- the count is owned instance state in a private field, with no public mutable static
field anywhere, so every
Counteris isolated and nothing outside can change it, - the composition root creates one
Counterper run and uses it, so each run starts from a clean count with nothing to reset, - the type is
sealed.
Keep the type named Counter, keep Next() returning the incremented value, and keep the
behavior tests green: same inputs, byte-identical output.
Ambient static state isn't fixed by lowering its visibility. It's fixed by giving it an owner. Find the rule the
private staticmove can't touch and let it tell you what "owned" really means.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Encapsulation
- Protect invariants: no exposed mutable collections, shared static state or train-wreck navigation.
Related katas
- History Keeps a Checkpoint, Not Editor Internals Medium Free Capture immutable mementos that can be restored repeatedly while the history caretaker stays unaware of the editor’s stored representation.
- Make It a Value Object Hard Free A Reservation is a mutable two-phase DTO that also hands out its backing guest list. Make it a real value object, frozen at construction, and don't let the obvious
private setfix fool you into leaving the collection leak wide open. - Seal the Aggregate Hard Free
Orderenforces a three-line cap inAddLinebut hands out its backingList<OrderLine>through a public, settable property. Seal the aggregate so callers can only change it through its methods, and don't let the obviousprivate setfix fool you into leaving the collection leak wide open. - A Preview Must Not Consume Work Easy Free Give queue observers a read-only capability and keep consumption behind an explicit command; repeated previews must leave the next item unchanged.