SOLID is an acronym for five object-oriented design principles popularized by Robert C. Martin, intended to keep code flexible and maintainable.
- Single responsibility: a class should have one reason to change.
- Open/closed: code should be open for extension but closed for modification.
- Liskov substitution: subtypes must be usable wherever their base type is expected.
- Interface segregation: prefer small, focused interfaces over large general ones.
- Dependency inversion: depend on abstractions, not concrete implementations.
Why it matters
The principles reduce coupling and make classes easier to test and replace. Dependency inversion underlies dependency injection, and single responsibility keeps classes small. They are guidelines rather than laws: followed dogmatically they can produce excessive interfaces and layers, and they are described mainly in class-based languages.
Example
A report class that fetches data, formats it and emails it breaks single responsibility. Splitting it gives ReportData, ReportFormatter and Mailer. If Mailer is an interface, tests can pass a fake mailer, which is dependency inversion in practice.
Common mistakes
- Creating an interface for every class with only one implementation.
- Reading single responsibility as "one method per class".
- Violating Liskov by overriding a method to throw "not supported".
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.