SOLID, with the refactor shown
SOLID is five design principles, and most explanations commit the same crime: they state each principle in abstract words and stop. "Classes should be open for extension but closed for modification." Cool — what does that look like? Useless without the picture. So here is each principle as a before-and-after: the code smell, the fix, and why the fix pays.
S — Single Responsibility: one reason to change
You met this in the last lesson; here's its classic crime scene. An Order class that calculates its total, saves itself to the database, and emails the receipt. Count the reasons it changes: pricing rules change (finance), the schema changes (data team), the email vendor changes (whoever drew the short straw). Three unrelated forces, one file, guaranteed collisions.
The refactor: split by reason. Order keeps state and business rules ("an order total is items minus discounts"). OrderRepository owns persistence. ReceiptNotifier owns delivery. Now the email vendor migration touches one small class and cannot possibly break pricing.
O — Open/Closed: extend without editing
The tell is a switch statement that grows a new case every time the business adds an option:
// closed to extension: every new payment type edits this method
switch (type) {
case CARD: return amt * 0.02;
case UPI: return 0;
case WALLET:return amt * 0.01;
}
// open: a new type is a NEW class; tested code is never touched
interface FeePolicy { double fee(double amt); }
class CardFee implements FeePolicy { ... }
class UpiFee implements FeePolicy { ... }
Why this matters isn't aesthetics — it's risk. In the switch version, adding "EMI" means editing a method that handles everyone's money, and re-testing every existing branch. In the interface version, you add a new class, test it in isolation, and the old code is untouched — it literally cannot have regressed.
The honest caveat: don't pre-abstract. Two cases? A switch is fine, clearer even. The interface earns its keep around the point where cases hit five-ish and change independently. Abstraction is a loan against future change; only take loans you'll use.
L — Liskov Substitution: subtypes must not surprise
If B extends A, then any code written for A must work with a B without knowing or caring. The famous violation: Square extends Rectangle. Seems logical — a square is a rectangle, right? But watch it break: code that does r.setWidth(5); r.setHeight(3) expects area 15. Hand it a Square, and setWidth(5) silently sets the height to 5 too (squares must stay square), so setHeight(3) gives area 9. The caller's perfectly reasonable assumption — width and height are independent — was violated by the subtype.
How to spot it in the wild: callers doing if (x instanceof Square). The moment code must check which subtype it got, substitution has already failed — the hierarchy is lying about what's interchangeable.
I — Interface Segregation: no forced irrelevance
A fat Machine interface declares print(), scan(), fax(). A humble printer must now implement scan() and fax() as... what, throw an exception? Every method that exists only to throw NotSupported is the interface confessing it's too fat. The fix: small interfaces — Printer, Scanner, Fax — and devices implement only what they truly do. Clients then depend only on the capability they use: a document pipeline that needs printing asks for a Printer, and doesn't break when the fax API changes.
D — Dependency Inversion: policy above detail
High-level business logic should not depend directly on low-level details. Concretely: OrderService should not import StripeClient. Instead, OrderService defines the interface it needs — PaymentGateway with a charge() method — and a StripeGateway implements it. The dependency arrow flips: the detail now depends on the policy's contract, not the other way round.
Two very practical payoffs: testing — you can test OrderService with a fake gateway, no network, no Stripe sandbox, milliseconds instead of minutes; and swapping — when the company renegotiates and moves to Razorpay, you write one new adapter class and business logic doesn't change by a single line.
Applying all five principles to a 40-line script and producing fourteen interfaces nobody needed. SOLID is a set of responses to pain you are actually feeling — collisions, risky edits, untestable code. If nothing has changed twice yet, you don't even know where the seams belong; guessing them in advance usually puts them in the wrong place. Feel the friction first, then cut the seam where it burns.