Four Layers for One Formula
Medium About 35 min Free, no account needed
A report-card service reaches its one weighted formula through a factory, a calculator contract, a base class, and a selector, each added so it could be swapped one day. Take out every layer nothing varies behind and keep the one abstraction that really varies, without changing a single report card.
The brief
Gradebook.ReportCardService writes one report-card line per student. To get a final score, it asks
a factory for a calculator. The factory builds the only calculator that has ever existed, which
inherits from a base class nothing else inherits from. To pick a grading scheme, it asks a selector
that has one implementation. Each layer was added so it "could be swapped later". Two years on,
none has been swapped, and a change to the formula means a tour of seven files.
Every interface is a promise that something can vary behind it. When nothing does, the interface is a cost with no payoff: every reader follows the extra hops, and behavior hides in the corners between them. That is speculative generality, and the fix is to take the abstraction out. Not every one, though. Credit students get a letter, audit students get pass or fail, and next term the international program adds a third scheme. The grading scheme really does vary.
The katas ask-for-the-interface and strategy-over-the-switch add an abstraction where something
varies. This one takes abstractions out where nothing does.
Each student arrives as <name>:<enrollment>:<homework>,<quizzes>,<exam>. Preserve every
report-card line, <name>:<final score>:<grade>, exactly as it is produced today:
- Each score is capped to the 0 to 100 range, then weighted: homework 20%, quizzes 30%, exam 50%.
- The total is rounded to one decimal, with halves rounded away from zero. The rounded score is the one that is printed and graded.
- A student whose enrollment is
audit, in any letter case, getspass(60 or more) orfail. Everyone else getsA(90 or more),B(80),C(70),D(60), orF.
Remove every abstraction that nothing varies behind, and keep the one that does. When you are done,
the factory contract, the calculator contract, the base class, and the selector contract are gone,
and every interface and abstract class left in your code has at least two real implementations.
ReportCardService is built as new ReportCardService(creditScheme, auditScheme) from two
IGradingScheme values and takes nothing else, so a new scheme plugs in without editing the service.
Keep LetterScheme, PassFailScheme, and StudentScores as they are, and keep
string Report(StudentScores student).
Your submission must (1) preserve every report-card line, including the hidden cases for capped
scores, rounding, and how the enrollment is spelled, and (2) satisfy every design rule shown in
the results: the service no longer reaches its formula or its scheme through the factory, the
calculator contract, or the selector, it still grades through IGradingScheme, both schemes still
implement it, and the service's fields are readonly.
A trusted caller then counts the concrete implementations of every interface and abstract class in
your code, including one grading scheme of its own, and fails the check if any has fewer than two, if
IFinalScoreCalculatorFactory, IFinalScoreCalculator, FinalScoreCalculatorBase, or
ISchemeSelector is still declared, or if a public constructor of ReportCardService takes anything
other than IGradingScheme values. Finally it builds the service from two grading schemes your code has never
seen, one for credit and one for audit, and checks that each student reaches the right one with the
final score after capping, weighting, and rounding.
An abstraction is paid for by its second implementation. Until that exists, it is a guess about the future that everyone who reads the code pays for today.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Speculative generality
- Remove layers and abstractions added for a future that never arrived.
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.
- Freeze the Money Medium Free Money pretends to be a value object: its arithmetic returns new instances, but its Amount and Currency are still publicly settable over a mutable field. Make it structurally immutable, and don't let the reflex
private setfool you; it hides the setter but leaves the backing field writable. - An OrderId Is Not a CustomerId Medium Free OrderBook and its three callers identify orders and customers with bare ints, so a swapped argument compiles and quietly files an order under the wrong customer. Give each identifier its own value type that only ever holds a positive number, and bring the callers along without reopening the hole.
- Kill the Global Counter Medium Free
Counterkeeps its running total in apublic staticfield 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 reflexprivate staticfix trick you into leaving the second smell standing.