One Reason to Change
Medium About 35 min Free, no account needed
PaymentImporter reads bank-statement lines, decides duplicates against its own dictionary, and words the summary, so a new statement column, a move to a database, and new finance wording all edit the same method. Split it along those reasons to change, and let the store decide what counts as a duplicate at the moment it stores a payment.
The brief
Payments.PaymentImporter.Import takes a batch of bank-statement lines, keeps the payments it has
not seen before, and returns a summary. It works. Three change requests are waiting for it:
- The bank is adding a currency column to its statement lines next quarter.
- Payments are moving into the ledger database. Its unique key will decide what counts as a duplicate, and that key may not stay "same reference" for long.
- Finance wants the summary worded differently for their new dashboard.
Today every one of those requests is an edit to the same method, reviewed by people who care about the other two. That is what the Single Responsibility Principle is about. It is not "a class does one thing". It is "a class has one reason to change", meaning one group of people whose requests drive its edits.
A line is reference,payer,amount. It is rejected when it does not have exactly three fields, when
the trimmed reference or payer is empty, or when the amount is not a positive number in invariant
culture. Today, a payment whose reference is already stored (compared exactly, across batches) is a
duplicate. Preserve the summary imported <n> (<total>), duplicates <n>, rejected <n> for each batch,
with the total formatted to two decimals.
Restructure the import so each of the three requests lands in one place and touches nothing else. Where the lines fall is your call, but the storage request sets one hard requirement: the store decides what counts as a duplicate, at the moment it is asked to store a payment. A payment is a duplicate exactly when the store refuses to keep it, and never because of a decision made anywhere else.
The grader finds your pieces by name, so use these:
Payment: an immutable parsed line withReference,Payer, andAmount.PaymentLineParser: the only code that knows how a statement line is laid out.IPaymentStore, the storage contract, andInMemoryPaymentStore, today's implementation of it. Shape the contract as you see fit, within one limit: each of its methods takes a singlePaymentor a singlestringand returnsbool. A method that stores aPaymentanswers whether the store kept it.ImportSummary: an immutable result withImported,Duplicates,Rejected, andTotal. It is data only and produces no text of its own.ImportSummaryFormatter: produces the summary text through one public method that takes anImportSummaryand returns astring, either static or on a formatter with a parameterless constructor.
PaymentImporter receives its IPaymentStore through its constructor and returns an
ImportSummary from Import(string[] lines). Solution picks the store, runs both batches through
one importer, and formats each summary.
Your submission must (1) preserve every summary, including duplicates across batches, trimming,
and exact reference comparison, and (2) satisfy every design rule shown in the results: the store
contract, parser, formatter, and immutable Payment and ImportSummary exist; the importer uses the
store contract and the parser but names neither the in-memory store nor the formatter; neither the
parser nor the formatter touches storage; and the importer's fields are readonly.
A trusted composition root then gives the importer a store your code has never seen. It answers whatever methods your contract declares, and it behaves like a database whose unique key is not the reference: it keeps at most one payment per payer, it enforces that only when a payment is stored, and it answers a question about a reference by whether that reference is stored. It records everything it is offered. The summary counts must match what that store answered when it was asked to store each payment, including a reference that repeats within one batch, and lines that fail parsing must never reach it.
A second trusted caller runs one import through another unknown store, hands the returned summary to
your ImportSummaryFormatter, and checks the text. It also fails if ImportSummary has a
hand-written ToString() or any other member that returns text, because a formatter that only
forwards to the summary has not taken the wording anywhere.
Ask who will request the next change to this code. If the answer is a list, the class is doing more than one job.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Single responsibility
- Give each class one reason to change.
- Extract class
- Move a cohesive set of fields and the behavior that uses them out of an overloaded class into its own.
Related katas
- Separate Terminal Replay Medium Pro Cursor motion, overwriting, tabs, and line emission are interleaved in one loop. Give the replay state a clear boundary.
- Split the DNS RRset Report Medium Pro DNS normalization, RRset aggregation, and alias diagnostics share one long method and four parallel dictionaries. Separate record identity from reporting.
- Split the Escaped Frame Decoder Hard Pro A serial protocol decoder mixes framing, escaped bytes, bounded buffering, checksum decisions, and chunk state in one method. Extract a recoverable parser without changing its wire behavior.
- Untangle the Rollout Reducer Hard Pro Generation checks and deployment transitions are tangled across parallel dictionaries. Preserve stale-event and terminal-state rules while separating the reducer.