Skip to content
Katabench
Try free

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 obvious move backfires

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.

Your job

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 Counter is isolated and nothing outside can change it,
  • the composition root creates one Counter per 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 static move 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.

SOLID principles exercises →

What it practices

Encapsulation
Protect invariants: no exposed mutable collections, shared static state or train-wreck navigation.

Related katas