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.
Interview question
In which scenario would rigorously applying all SOLID principles most likely lead to over-engineering rather than benefit?
- a.Working on a system with a large team and a long maintenance lifecycle.
- b.Developing a complex, multi-module enterprise application.
- c.Creating a simple, single-purpose command-line tool or prototype.Correct
- d.Building a new feature into an existing, well-structured codebase.
Why? this is the answer
The card explicitly states that for "very small projects, prototypes, or throwaway scripts, rigorously applying all five principles can lead to premature optimization and unnecessary complexity." The other options describe scenarios where SOLID principles are highly beneficial for managing complexity and ensuring maintainability.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #solid
- #oop
- #design principles
- #architecture
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles