SOLID Principles

Five guidelines, grouped under one acronym, for keeping object-oriented code flexible and easy to change.

What is it?

What do separation of concerns and low coupling/high cohesion look like in everyday object-oriented code? In the late 1990s and 2000s, engineers kept seeing the same five design problems, and named a guideline for each. Together they spell SOLID.

- Single Responsibility Principle (SRP): a class or module should have only one reason to change. A ReportGenerator that calculates data and formats it as PDF has two: a math bug and a formatting request. Split it.

- Open/Closed Principle (OCP): code should be open for extension but closed for modification. You add new behavior (say, a new payment method) without editing tested code for the old ones, usually by adding a new implementation of a shared interface instead of another if/else branch.

- Liskov Substitution Principle (LSP): code that works with a base type (like Bird) should keep working with any subtype (like Penguin). A classic violation: Penguin extends Bird where Bird has a fly() method Penguin can't honestly implement.

- Interface Segregation Principle (ISP): don't force a class to implement methods it doesn't need because they're bundled into one big interface. Many small, focused interfaces beat one giant one that every implementer partly fakes.

- Dependency Inversion Principle (DIP): high-level logic should depend on an abstraction, not a concrete low-level detail. It gets its own topic next.

These aren't laws. They're guidelines drawn from what makes object-oriented code brittle, and each is a concrete case of "low coupling, high cohesion".

Explain like I'm 10

SOLID is like five tips from a woodworking mentor: 'each tool has one job', 'add attachments instead of modifying the tool', 'a replacement blade should fit the same slot', 'don't bundle unrelated tools into one handle', and 'design your workbench around the kind of tool, not one brand.'

Examples

SRP: one class, one reason to change

// Violates SRP: two reasons to change (data logic + formatting)
class Invoice {
  calculateTotal() { /* math */ }
  printAsPdf() { /* formatting */ }
}

// Follows SRP: split by responsibility
class Invoice {
  calculateTotal() { /* math */ }
}
class InvoicePdfPrinter {
  print(invoice) { /* formatting */ }
}

A PDF layout fix no longer risks the invoice math, and vice versa. Each class has exactly one reason to change.

OCP: adding behavior without editing existing code

// Violates OCP: every new payment method edits this function
function pay(method, amount) {
  if (method === "card") return payWithCard(amount);
  if (method === "paypal") return payWithPaypal(amount);
  // adding "crypto" means editing this function again
}

// Follows OCP: new methods extend, don't modify
const paymentMethods = {
  card: payWithCard,
  paypal: payWithPaypal,
};
function pay(method, amount) {
  return paymentMethods[method](amount);
}
// Adding crypto: paymentMethods.crypto = payWithCrypto; — no edits above

The pay function never changes again to support a new method. New behavior is added next to it, not by editing it.

How it works

SOLID is less a checklist than five lenses. For any class or module, ask:

  • Does it have one reason to change (SRP)?
  • Could I add a new case without editing it (OCP)?
  • Would a subtype break code written for the base type (LSP)?
  • Is any implementer forced to support methods it doesn't need (ISP)?
  • Does it depend on a concrete detail instead of an abstraction (DIP)?

Most real design smells map onto one of these questions.

Why does it exist?

Without these principles, object-oriented codebases develop the same recurring problems:

  • God classes that do everything (SRP).
  • Growing if/else chains that break something with each new case (OCP).
  • Subclasses that quietly break assumptions other code relies on (LSP).
  • Bloated interfaces nobody fully implements (ISP).
  • Business logic hard-wired to one database or library (DIP).

Naming each problem made it easier to spot and discuss.

When to use it

Use them as a design review lens on object-oriented code that will grow, especially before adding "just one more" branch to a function that already has several, or before subclassing. They matter most when you're deciding how to extend existing code.

When not to use it

Applying all five rigidly to every tiny class, even ones that will never grow or vary, creates needless abstraction: interfaces and extension points for cases that never come. SOLID answers expected change. Where none is expected, simple direct code wins.

Common mistakes

  • Treating SOLID as five separate rules instead of five sides of one goal: code that's easy to change safely.

  • Applying Open/Closed so hard that every function gets an interface 'just in case', even with no second implementation ever.

  • Thinking Liskov Substitution means 'subclasses must override every method'. LSP is about behavioral compatibility, not just matching method signatures.

Practice exercises

  1. Easy:

    Split a class that validates user input and saves it to a database so it follows the Single Responsibility Principle.

  2. Medium:

    Refactor an if/else chain that picks a shipping-cost calculation from a 'carrier' string into a design that follows the Open/Closed Principle.

  3. Hard:

    A Rectangle class has setWidth/setHeight, and a Square class extends it by keeping width and height equal. Explain why this violates the Liskov Substitution Principle, and propose another design.

Interview questions

What does the 'S' in SOLID stand for, and what does it mean?

Single Responsibility Principle: a class or module should have only one reason to change, meaning one clearly defined job.

What's the difference between the Open/Closed Principle and just writing flexible code?

OCP specifically means you add new behavior by adding new code (e.g., a new class implementing a shared interface), without modifying and re-testing existing working code.

Can you give an example of a Liskov Substitution Principle violation?

A Square extending a Rectangle with independent setWidth/setHeight. Setting a Square's width must also change its height, so code written for any Rectangle can behave unexpectedly when given a Square.