Track 04 · Lesson 2 of 15Code that stays easy to change

Track 04 · Code that stays easy to change

SOLID principles at a glance

Lesson 2 of 15· 7 min

SOLID is five principles for code that keeps working while it changes, one per letter. Here’s each in one line and one checkout module that breaks all five. Fix them letter by letter, then the next five lessons go deep on each.

Fix the checkout, one letter at a time

Pick a letter, read the symptom, apply the fix

One checkout module with five problems, one per letter. Pick a letter, read the symptom, then apply the fix.

S · Single responsibility

checkout.tsx is 214 lines and four teams edit it: design, payments, tax and analytics.

Without it

// checkout.tsx (214 lines)
export function Checkout() {
  // layout, Stripe calls, VAT rules, analytics…
}

4

Teams editing one file

0 of 5 fixed. Start with any letter.

S: Single responsibility#

A module should have one reason to change, meaning one group of people who ask for changes to it. When design, payments and analytics all edit the same file, every change risks the others. Full lesson.

O: Open/closed#

Open for extension, closed for modification: new behavior should arrive as new code beside tested code, not as edits inside it. A switch that grows with every payment method is the classic smell. Full lesson.

L: Liskov substitution#

Anything that claims to be a kind of something must work everywhere that something is used. A subclass that throws “not supported” breaks the promise its parent made. Full lesson.

I: Interface segregation#

Many small interfaces beat one big one. Nobody should have to implement, or depend on, methods they don’t use. Full lesson.

D: Dependency inversion#

High-level code depends on an interface, not on a concrete tool, and the real tool is plugged in from outside. That’s what lets tests use a fake mailer and lets you switch providers in one line. Full lesson.

Where SOLID came from#

Robert C. Martin collected these principles for object-oriented design around 2000, and Michael Feathers noticed their first letters spelled SOLID. They’re not only about classes: the same ideas apply to React components, functions and modules, which is how this track teaches them.

Which letter?

Tag each code review with the letter it breaks

Five code reviews, five problems. Tag each with the letter it breaks.

  1. OrderService imports PostgresClient directly, so you can’t test it without a database.

  2. MutableList.add() works, but FrozenList, a subclass, throws when you call add().

  3. ReportService builds the PDF, emails it and writes the audit log.

  4. Your Repository interface has 14 methods, and the cache only ever calls get().

  5. Supporting a new export format means editing exportFile() yet again.

0 of 5 tagged correctly.

Check yourself

Tests for signUp() send real emails, because signUp() creates its own email client. Which letter is broken?