SOLID Design Principles

Five principles for code that stays easy to change as it grows.

SOLID is a set of five object-oriented design principles described by Robert C. Martin. Their shared goal is code that can change without breaking: small, focused pieces with clear boundaries and few surprising dependencies.

The principles apply well beyond classes. The same ideas shape good modules, services and APIs, and they make code easier for both people and AI assistants to understand and modify safely.

The five principles

S: Single responsibility
A module should have one reason to change. Separate, for example, billing calculations from invoice formatting.
O: Open/closed
Open for extension, closed for modification: add new behaviour by adding code, such as a new payment provider class, rather than editing existing logic.
L: Liskov substitution
Subtypes must be usable wherever their parent type is expected, without surprises.
I: Interface segregation
Prefer small, specific interfaces over large ones, so clients depend only on what they use.
D: Dependency inversion
Depend on abstractions rather than concrete implementations, so details such as databases can change without rewriting business logic.

Why they matter

Code that follows SOLID tends to have smaller, more focused changes, fewer unexpected side effects, and tests that are easier to write because dependencies can be replaced with test doubles. That lowers the cost of change, which is the real goal of good design.

Beyond object orientation

Single responsibility applies to services and teams: a service owned by one team, for one business capability. Interface segregation applies to APIs: smaller, purpose-built endpoints instead of one endpoint that does everything. Dependency inversion underpins hexagonal and clean architectures, which keep business logic independent of frameworks and infrastructure.

Applying them pragmatically

Principles are guides, not laws. Applied dogmatically, they produce layers of interfaces and abstractions that make simple code hard to follow. Introduce abstractions when a second real use case appears or when testing needs them, not in anticipation of every possible future change.

SOLID and AI-generated code

AI assistants generate code faster than people can review it, so structure matters more. Well-separated modules with clear interfaces let an assistant change one part without touching others, and make reviews easier. Including your design conventions in the repository's context file helps assistants follow them.

A quick example

Imagine an order service that calculates totals, applies discounts, charges a card and sends an email, all in one class. Every change to pricing, payment or notifications touches the same file, and testing any part means setting up all of them.

Applying SOLID splits it into a pricing component, a payment port with provider adapters, and a notification component, connected through small interfaces. Adding a new payment provider now means adding one adapter, and each part can be tested on its own.

How to put it into practice

  1. Use code reviews to ask "what is this module's one reason to change?"
  2. Extract interfaces at boundaries with external systems such as databases and APIs.
  3. Add new behaviour through extension points rather than growing conditional logic.
  4. Keep interfaces small and named after what clients need.
  5. Refactor towards SOLID when touching code, rather than in big rewrites.
  6. Document design conventions in the repository context file.

Common mistakes

  • Creating an interface for every class whether or not it is needed.
  • Huge "manager" or "utility" classes that do everything.
  • Subclasses that break the expectations of their parent type.
  • Treating the principles as rules to win arguments rather than tools for clarity.

How to measure progress

  • Average size and change frequency of modules (hotspots).
  • Changes that touch many unrelated files.
  • Ease of writing unit tests for new code.
  • Defects introduced by changes in unrelated areas.

Go further