← Study Notes Programming Fundamentals


Programming Fundamentals

SOLID

Five object-oriented principles — Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion — that keep code loosely coupled and open to change. They push you toward small interfaces and injected dependencies. Over-applied they cause premature abstraction, so treat them as guidance, not law.


Purpose

SOLID is five design principles for keeping object-oriented code loosely coupled and changeable: Single Responsibility (one reason to change), Open/Closed (extend without modifying), Liskov Substitution (subtypes must honour the base contract), Interface Segregation (small, client-specific interfaces) and Dependency Inversion (depend on abstractions, not concretions).

When to Use It

They guide everyday decisions: splitting a class that mixes persistence with business rules (SRP), adding a new payment provider without editing the checkout (OCP), and injecting a repository interface so logic can be tested without a database (DIP). They shine most in long-lived codebases with many contributors.

Trade-offs

Over-applied, SOLID produces premature abstraction — interfaces with one implementation, indirection nobody needed. The principles describe forces, not laws; the cost of violating them is real only when the code actually changes along that axis.

Implementation

Let the principles surface during refactoring: when a change forces edits in many places, apply SRP or DIP to isolate the volatile part. Introduce an interface when a second implementation or a test double genuinely appears, and use Liskov as the acid test for whether inheritance is legitimate.