The SOLID principles in plain English
Five principles for code that is easy to change, each explained with a short example and a warning about overuse.
Key takeaways
- SOLID is about making change cheap and safe.
- Each principle reduces the ripple effect of a change.
- Apply SOLID where code changes often, not everywhere.
SOLID is a set of five design principles collected by Robert C. Martin ("Uncle Bob"). They all aim at the same goal: when requirements change, as few things as possible should have to change with them.
#S: Single Responsibility Principle
A module should have one reason to change.
InvoiceService that calculates totals, renders PDFs and sends emails has three reasons to change. Split it: InvoiceCalculator, InvoicePdfRenderer, InvoiceMailer.
Smell: a class name with "And" or "Manager" in it, or a file everybody edits.
#O: Open/Closed Principle
Open for extension, closed for modification.
Add new behavior by adding code, not editing working code. The Strategy pattern is the classic tool: a new payment method is a new class, and checkout stays untouched.
#L: Liskov Substitution Principle
Subtypes must be usable wherever their base type is expected.
If Square extends Rectangle breaks code that sets width and height independently, the hierarchy is wrong. Rule of thumb: a subtype may accept more and promise more, never less.
#I: Interface Segregation Principle
Many small interfaces beat one big one.
// Too big: a read-only report must implement save() and delete()
interface Repository { find(); save(); delete(); export(); }
// Better
interface Reader<T> { find(id: string): Promise<T>; }
interface Writer<T> { save(item: T): Promise<void>; }
#D: Dependency Inversion Principle
Depend on abstractions, not concrete details.
Business logic should depend on an OrderRepository interface, not directly on Postgres. The database adapter implements the interface. This makes testing easy and is the foundation of hexagonal architecture.
#When SOLID goes wrong
- Interface explosion: an interface for every class, each with one implementation forever.
- Abstraction astronautics: five layers of indirection for a CRUD endpoint.
- Dogma in code review: "This violates SRP" without explaining the real cost.
Tip: Apply SOLID in proportion to how often the code changes. Stable, simple code can stay simple.
#SOLID and AI-generated code
AI assistants often produce large, do-everything functions. Ask explicitly: "Split this into small units with single responsibilities and inject dependencies." Then review whether the split actually makes sense.
Keep reading
The small daily habits that make code easy to read, review and change.
How many tests of which kind, how to write tests that catch real bugs, and how AI changes testing.
How to make code reviews fast, kind and genuinely useful, including reviews of AI-generated code.