SOLID Principles
1. Single Responsibility Principle (SRP)
Definition. A module should have one reason to change: it owns a single responsibility and the knowledge behind it.
Rationale. When a unit does several unrelated things, a change to one forces re-testing and re-deploying the others, and the unit accumulates unrelated dependencies. Splitting by responsibility keeps change impact small and makes the code easier to name, test, and replace.
Example. An Invoice class that both computes totals and renders PDFs has two reasons to change (tax rules vs. layout). Split it into Invoice (totals) and InvoicePrinter (rendering); the printer depends on the invoice, not vice versa.
Anti-pattern / failure mode. A “god object” or service class that grows a new method for every feature request, with unrelated fields and imports piling up. Review signal: a class with many unrelated public methods or frequent merge conflicts.
Related. Separation of Concerns (#9), High Cohesion, Low Coupling (#28), Modularity (#29).
2. Open/Closed Principle (OCP)
Definition. Software entities should be open for extension but closed for modification: new behavior is added by writing new code, not by editing stable, tested code.
Rationale. Editing working code risks regressions and forces re-testing everything that depends on it. Extension points (interfaces, hooks, strategies) let you add capabilities while leaving proven code untouched.
Example. Instead of adding if (type == "pdf") branches to a ReportExporter, define an Exporter interface and add PdfExporter as a new implementation. The exporter loop never changes.
Anti-pattern / failure mode. Repeated switch/if-else chains on a type tag that grow with every new variant; each new case edits a central function. Review signal: a function modified in every feature PR.
Related. Strategy pattern, Dependency Inversion (#5), Composition over Inheritance (#12).
3. Liskov Substitution Principle (LSP)
Definition. Subtypes must be substitutable for their base types without breaking correctness: a caller using the base type must not be able to tell which subtype it got.
Rationale. Inheritance is a behavioral contract, not just code reuse. If a subtype strengthens preconditions, weakens postconditions, or throws where the base did not, polymorphic code silently breaks. Honoring the contract keeps abstraction honest.
Example. A Square subclass of Rectangle that overrides setWidth/setHeight to keep sides equal violates callers that set width and height independently. Prefer a common Shape interface instead.
Anti-pattern / failure mode. Overriding methods to throw UnsupportedOperationException, or narrowing return types/accepting fewer inputs than the base. Review signal: instanceof checks scattered in client code.
Related. Program to an Interface (#15), Design by Contract, Interface Segregation (#4).
4. Interface Segregation Principle (ISP)
Definition. Clients should not be forced to depend on interfaces they do not use. Split wide interfaces into smaller, focused ones.
Rationale. A fat interface couples every client to methods it never calls, so a change to one method forces recompilation and re-testing of all clients. Narrow interfaces keep dependencies precise and evolution independent.
Example. Instead of one Worker interface with work() and eat(), split into Workable and Feedable. A robot implements only Workable.
Anti-pattern / failure mode. A single interface accumulating methods for every consumer (“kitchen sink” interface), where most implementers leave several methods as no-ops. Review signal: many UnsupportedOperationException stubs.
Related. Single Responsibility (#1), LSP (#3), High Cohesion, Low Coupling (#28).
5. Dependency Inversion Principle (DIP)
Definition. High-level and low-level modules should depend on abstractions, not on concrete implementations. Invert dependencies toward stable contracts.
Rationale. If high-level policy depends on low-level details, the policy changes whenever details change. Depending on abstractions keeps the core stable and lets details vary (databases, frameworks, devices) without touching business rules.
Example. An OrderService should depend on a PaymentGateway interface, not on StripeClient. Inject the concrete gateway at the composition root.
Anti-pattern / failure mode. new-ing concrete dependencies inside business logic, or importing database/HTTP clients directly into domain classes. Review signal: domain modules that cannot be unit-tested without real infrastructure.
Related. Hexagonal Architecture (#23), Clean Architecture (#24), Program to an Interface (#15).