When writing clean and maintainable code, the Single Responsibility Principle (SRP) is one of the most important design principles to follow. It is the first principle in the SOLID design principles, which are a set of five rules that help developers write better object-oriented code.
In this article, you’ll learn:
- What the Single Responsibility Principle is
- Why it matters
- Real-world and code examples
- Benefits of using SRP
- Best practices to follow
What is the Single Responsibility Principle?
The Single Responsibility Principle (SRP) states:
“A class should have only one reason to change.“
This means that a class should do only one thing and should have only one responsibility. In other words, it should only focus on one task or job.
If a class is doing multiple unrelated tasks, then it violates SRP and becomes harder to understand, test, and maintain.
Real-World Analogy
Imagine you own a house and hire a cleaner, a plumber, and an electrician. Each one has a specific responsibility:
- The cleaner cleans the house.
- The plumber fixes water leaks.
- The electrician repairs electric faults.
Now, imagine one person doing all three jobs. If anything goes wrong, it’s harder to pinpoint the issue or fix just one part without affecting the others.
The same concept applies in programming.
Code Example Without SRP (Bad Practice)
class Report {
public:
void generateReport() {
// Logic to generate the report
}
void printReport() {
// Logic to print the report
}
void saveToFile() {
// Logic to save the report to a file
}
};
What’s wrong here?
This class is doing too much:
- Creating a report
- Printing the report
- Saving the report
These are different responsibilities, and each one might change for a different reason.
Code Example With SRP (Good Practice)
class ReportContent {
public:
void generateReport() {
// Only generates the report content
}
};
class ReportPrinter {
public:
void printReport() {
// Only responsible for printing
}
};
class ReportSaver {
public:
void saveToFile() {
// Only responsible for saving
}
};
Now, each class does only one job, and if anything changes, you only need to modify that specific class. This makes the code clean, testable, and easy to maintain.
Why is SRP Important?
- ✅ Improves readability – Each class has a clear and focused purpose.
- ✅ Simplifies testing – You can test each class independently.
- ✅ Reduces bugs – Changes in one class don’t affect others unexpectedly.
- ✅ Supports reusability – Classes with a single purpose are easier to reuse.
- ✅ Enhances maintainability – Easier to fix issues or update code later.
How to Identify SRP Violations
Ask yourself:
- Does the class do more than one thing?
- Can I split this into smaller, more focused classes?
- Are there multiple reasons this class might change?
If the answer is yes, then it’s time to refactor your code using SRP.
Best Practices to Follow
- 🧠 Focus each class on one and only one responsibility.
- 🧪 Write unit tests to check if a class is tightly focused.
- 📂 Organize files and folders based on responsibilities.
- 🧹 Keep your methods short and clear.
- 🔁 Refactor often when a class starts growing too large.
Conclusion
The Single Responsibility Principle is all about keeping your code clean, organized, and focused. By giving each class a single job, you make your codebase easier to understand, test, and maintain — especially as your project grows.
You can also Visit other tutorials of Embedded Prep
- What is eMMC (Embedded MultiMediaCard) memory ?
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Little Endian vs. Big Endian: A Complete Guide
- Merge sort algorithm
Special thanks to @mr-raj for contributing to this article on Embedded Prep
Leave a Reply