LOAD-BEARINGfree
Low-level designTier 2 · unit 1 of 5 · 12 min read

Responsibility and the one-reason test

We're zooming in. High-level design asks "which servers and databases?" Low-level design asks a smaller but equally hard question: inside one service, which objects exist, what does each one know, and what does each one do? Get this wrong and your code becomes the kind where every change breaks three unrelated things — you've worked in that codebase; everyone has. Let's learn why it happens and how to prevent it.

Start with nouns — then interrogate them

Take the classic interview problem: "design a parking lot." Step one is listing the nouns, and it's easy: ParkingLot, Level, Spot, Vehicle, Ticket, Payment. Thirty seconds, done. Here's the thing beginners miss: the noun list gets you nowhere by itself. The actual design work is deciding the relationships and rules — who is responsible for what. Watch:

  • Who decides which spot a vehicle gets? The lazy answer: ParkingLot.park() figures it out. But allocation rules multiply — nearest to entrance, electric vehicles near chargers, reserved spots, disabled access — and that method grows to 300 lines of tangled if-statements. Better: extract a SpotAllocationStrategy, a separate object whose entire job is answering "which spot?"
  • Who computes the parking fee? Tempting: Ticket.calculateFee() — the ticket has the times, after all. But think about what a ticket is: a record of fact. It stores what happened — entry time, spot, vehicle. Facts don't compute; rules compute. A PricingStrategy calculates the fee; the ticket just stores the result. Records record; strategies decide.
  • The litmus test: what happens when the fee structure changes? If a pricing change forces you to edit ParkingLot, your responsibilities are wired wrong — the lot shouldn't know anything about money.

The one-reason test

Here's the principle underneath all of this, and the most useful sentence in low-level design: a class should have one reason to change.

Notice it's not "one method" or "one job" vaguely defined — it's one source of change requests. Concretely: pricing changes come from the finance team; allocation changes come from operations. If both kinds of change land edits in the same class, two stakeholders with unrelated agendas are editing the same file, their changes will collide, and every pricing tweak risks breaking allocation. Separate the reasons, and each change touches exactly one small class. That's the entire economic argument for good design: cheap, safe change, forever.

Tell, don't ask

A second principle, best shown side by side:

// ASKING: caller pulls the state out and decides for the object
if (account.getBalance() >= amt) {
  account.setBalance(account.getBalance() - amt);
}

// TELLING: the object protects its own rule
account.withdraw(amt);   // throws InsufficientFunds

Spot the deep problem with the first version: the rule "you can't withdraw more than you have" now lives in the caller. Which means every caller — every screen, every batch job, every future feature — must remember to repeat that check. One day, one of them forgets. That's not a maybe; across enough callers and enough years, it's a certainty. (There's also a hidden race condition between the check and the write — the balance can change in between. That bug gets its own lesson, 2.05.)

The second version has exactly one place that can be wrong. The account guards its own invariant — a rule about its state that must always hold. Objects that protect their own rules are the whole point of object-oriented programming. Objects that just hold data while everyone else pokes at it are the failure mode, and the failure mode has a name:

The trap

Anaemic objects: classes that are nothing but getters and setters, with all the actual logic dumped into a giant SomethingService class. That's not object-oriented design — it's a struct with extra ceremony, and it guarantees the "asking" problem everywhere: every rule scattered across every caller. The cure is always the same: find the data, find the rules about that data, and put them in the same class. Behaviour belongs with the data it protects.