SOLID Principles: Writing Maintainable Object-Oriented Code
SOLID principles are five design rules for writing maintainable object-oriented code. They guide you toward creating flexible systems that are easy to change. The footgun is treating them as rigid laws, leading to over-engineered and complex solutions.
THE MENTAL MODEL: SOLID is a set of five design principles that act as a guide for managing dependencies in object-oriented code. Think of them as guardrails against creating a "big ball of mud"—a system so interconnected and rigid that a small change in one place breaks things in five other unexpected places. The goal is to create code that is understandable, flexible to new requirements, and maintainable over the long term.
HOW IT WORKS: SOLID is a mnemonic for five distinct principles. First, the Single Responsibility Principle (SRP) states a class should have only one reason to change, meaning it has one specific job. Second, the Open/Closed Principle (OCP) says software entities should be open for extension but closed for modification; you add new functionality by writing new code, not by changing old, working code. Third, the Liskov Substitution Principle (LSP) requires that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. Fourth, the Interface Segregation Principle (ISP) advises using many small, client-specific interfaces rather than one large, general-purpose one. Finally, the Dependency Inversion Principle (DIP) dictates that high-level modules should not depend on low-level modules; both should depend on abstractions like interfaces.
WHEN TO USE IT: Apply SOLID principles when designing software that you expect to evolve. They are essential for medium to large-scale applications, especially those with long lifespans and multiple contributors. Following these guidelines makes the system more modular, easier to test in isolation, and less fragile when new features are added or requirements change. They are a core philosophy in agile development for this reason.
WHEN NOT TO USE IT: Treat SOLID as a set of guidelines, not absolute laws. For very small projects, prototypes, or throwaway scripts, rigorously applying all five principles can lead to premature optimization and unnecessary complexity. Forcing dependency inversion and creating interfaces for every class in a simple command-line tool is likely over-engineering. The cost of the added abstraction may not be worth the benefit in short-lived or simple codebases.
ONE CANONICAL EXAMPLE: A classic violation of the Liskov Substitution Principle is the square-rectangle problem. It seems intuitive for a Square class to inherit from a Rectangle class. However, a Rectangle might have separate setWidth and setHeight methods. A Square must maintain equal sides, so setting its width must also set its height. A piece of code that takes a Rectangle and sets its width to 10 and height to 20 would get an unexpected result if passed a Square, which would end up as 20x20. This breaks the substitutability rule.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.