SOLID
SOLID is a set of five design principles that help developers change one part of a program without having to rewrite unrelated code. They’re most often used in object-oriented design.
What SOLID is for
Imagine adding a new way to export a report. Should that change touch the code that calculates the totals? Usually not: the output format and the calculation follow different rules. SOLID gives developers five ways to spot this kind of coupling and decide where to put a boundary. The principles are design guidelines, not a checklist every class must pass.
What the five letters mean
- S — Single Responsibility Principle. Put code together when it changes for the same reason. A billing module can have several methods that enforce billing rules, but it need not also own the layout of a printed invoice. Those requirements can change independently.
- O — Open/Closed Principle. For a change you expect, make it possible to add behavior without rewriting the stable code that uses it. Adding a CSV formatter should not require editing the existing JSON formatter or the export workflow. Some wiring still has to change so the application can select the new formatter.
- L — Liskov Substitution Principle. Code written against a type should still work correctly when it receives a subtype. Matching method signatures is not enough: if the contract accepts a report with no rows, an implementation cannot demand at least one row.
- I — Interface Segregation Principle. Give callers interfaces that match what they need. A dashboard that only reads reports should not depend on an interface that also lets administrators edit and delete them.
- D — Dependency Inversion Principle. High-level policy should not depend directly on low-level details. Have the policy use an abstraction that expresses what it needs, then make the database or network adapter conform to it.
Example: where a report gets saved
Suppose an export function creates a LocalFileStore internally. Adding cloud storage now means editing the export workflow to choose between destinations. Instead, the function can accept a ReportStore defined in terms of what export needs:
interface ReportStore {
save(name: string, contents: string): Promise<void>;
}
async function exportReport(report: Report, store: ReportStore) {
await store.save(report.name, renderCsv(report));
}
The application chooses the concrete store when it wires up the export workflow. That keeps storage details out of the workflow (DIP) and lets you add a destination without editing the function (OCP). Both stores must still meet the same behavioral contract. If save promises to reject on failure, a cloud adapter that silently swallows errors is not a valid substitute (LSP).
When to use it and how it comes up in interviews
Look for changes that repeatedly spill into unrelated code. If adding a storage destination is a real requirement, a storage interface can pay for itself. If there is only one implementation and no useful boundary to protect, adding an interface may just give readers another file to open. Similarly, a small switch over a genuinely fixed set of options is not automatically a design problem.
In an interview, you may be asked to review a snippet or sketch a refactor. Name the change that is hard to make, show which dependency or contract causes the problem, and explain what your proposed boundary buys at the cost of extra indirection.