Skip to content
Katabench
Try free

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, gets pass (60 or more) or fail. Everyone else gets A (90 or more), B (80), C (70), D (60), or F.

Your job

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

How it's graded

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.

SOLID principles exercises →

What it practices

Speculative generality
Remove layers and abstractions added for a future that never arrived.

Related katas