SOLID principles matter because they help keep code easy to change. When a class takes on too many jobs, or depends directly on details that change often, a small edit can break parts of the program that seem unrelated. SOLID is a set of five guidelines for object-oriented code that address these problems. They work in C++ as well as in other languages.
In short, SOLID principles make code easier to change, test and reuse. They ask you to keep each class focused on one job, to add behavior through new code rather than edits to old code, and to depend on interfaces instead of specific implementations. They are guidelines, not laws, so they work best when you understand the problem each one solves.
The problems they address
Code tends to become hard to change in a few predictable ways:
- A class does several unrelated jobs, so a change to one job risks breaking the others.
- A class creates its own dependencies, so it is hard to test without real ones, such as a database or an email server.
- Adding a new case means editing a long chain of
iforswitchstatements, which can break code that already worked. - A subclass behaves differently from its base class in ways that surprise the caller, or an interface forces classes to implement methods they never use.
Each principle targets one of these problems:
| Principle | Problem it targets |
|---|---|
| Single responsibility | A class with several unrelated reasons to change |
| Open/closed | Editing working code every time behavior is added |
| Liskov substitution | Derived classes that break what callers expect |
| Interface segregation | Large interfaces with methods that no one needs |
| Dependency inversion | High-level code tied to specific low-level details |
A small example of tight coupling
Here is a class that creates its own dependency:
#include <iostream>
#include <string>
class EmailSender {
public:
void send(const std::string& to, const std::string& text) {
std::cout << "Email to " << to << ": " << text << "\n";
}
};
class OrderService {
public:
void placeOrder(const std::string& customer) {
EmailSender sender; // OrderService builds its own dependency
sender.send(customer, "Your order is confirmed.");
}
};
This code runs, but testing placeOrder means sending a real email. Switching to SMS would mean editing placeOrder. If the class receives an abstract sender through its constructor instead, you can test it with a fake sender and add an SMS option without touching placeOrder. This is the idea behind dependency injection in c, and the full pattern is part of solid principles in c.
What the principles do not promise
- They do not guarantee correct code.
- They can add classes and indirection. A small program with a few functions may be easier to read without them.
- Their benefit usually grows with the size of the code, the number of people working on it and how often requirements change.
Where to start
Single responsibility is usually the easiest principle to notice in everyday code, so it is a good first step. Once you can spot a class with mixed jobs, the other principles are easier to follow. For code examples of each one, see solid principles in c.