Bookmarks

SOLID — SOLID principles

Five object-oriented design principles that help keep code flexible: modules change for one reason, and behaviour is extended with new code without touching the old.

Also: solid principles, SOLID principles, five principles of object-oriented design

checked 8 Nov

All terms

Five rules of object-oriented design set out by Robert Martin: he analysed the OCP, LSP, DIP and ISP principles in his 2000 article “Design Principles and Design Patterns”, and formulated SRP in the late 1990s. Each rule fights code “rot” — a state where a small change triggers a cascade of changes in other modules, and the code becomes ever more expensive to maintain.

The five principles

AbbreviationPrincipleEssence
SRPSingle Responsibility Principlea module has one reason to change — one responsibility to one group of users
OCPOpen-Closed Principlebehaviour is extended by adding new code rather than rewriting working code; formulated by Bertrand Meyer in 1988
LSPLiskov Substitution Principlea subtype works anywhere the base type is expected without breaking the program; the idea was proposed by Barbara Liskov in 1987
ISPInterface Segregation Principleseveral narrow interfaces are better than one wide one, so that clients do not depend on methods they do not need
DIPDependency Inversion Principlecode depends on abstractions, not on concrete classes: “depend on abstractions, not on concretions”

The SOLID acronym is made up of the first letters of the principle names. Martin clarified that a single responsibility is not “one line of code” but one group of people asking for changes: if a class calculates salaries and builds reports, a change to the calculation may accidentally break the report — and both sides end up unhappy.

Example: dependency inversion

pythonDIP: the exporter does not know where the invoices come from
            class InvoiceGateway(Protocol):                def get(self, invoice_id: int) -> Invoice: ...             class PdfExporter:                def __init__(self, gateway: InvoiceGateway) -> None:                    self.gateway = gateway  # depends on the abstraction, not on the database class            

Business rules import neither the database class nor the HTTP client — an interface is enough for them. In production an implementation backed by PostgreSQL arrives; in tests, a fake built from a dictionary. This is how DIP and SRP work together: report logic changes independently of storage. Clean and hexagonal architectures build on DIP, with the core unaware of the details.

How to apply them without going overboard

The principles concern classes and modules inside a component, not boundaries between components: cohesion and coupling govern the ties between parts of a system. Do not carve out interfaces for every client without need. Introduce abstractions where a second behaviour is genuinely foreseen, not “just in case”.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • design patterns

    SOLID is a set of rules for arranging classes so that code is easy to change; design patterns are ready-made solutions to typical design problems.

  • DIdependency injection

    DI is the mechanism for passing dependencies through a constructor or parameters; DIP is the rule that one should depend on abstractions.

In depth in the references

where the topic is covered in detail