Single Responsibility Principle (SRP)

In this article, we are going to discuss SRP which is one of the SOLID design principles. S in SOLID denotes the Single responsibility principle. It is a recommended practice to implement it in our code to make the code leaner and less coupled.

What is SRP?

Each software module or class should have only and only one reason to change.

Robert C. Martin

We need to design the software so that every class has one or a similar set of functions. For example, we are designing a module in which the user gets a welcome email on a sign-up and all the user actions are logged. If we write all of these functions in a single class then it would become really complex. In the future, a single change would impact the whole class and can break things.

To make the code manageable, which is one of the principles of OOP. We would split the functionality into multiple classes. So each class has a single function to perform thus resulting in a maintainable software.

Without SRP

As you can see, the User module/class is doing multiple things. It is saving a user, sending an email, and logging all the activity. It is violating the Single Responsibility Principle. If we don’t change the code then we would have to copy the email/log activity code to all the other classes where it is needed. User class should only do one thing which to save users to the database.

With SRP

With SRP you can see each class has its own set of function(s). Users can communicate with databases, email class is responsible for emails and logging class to log activity. As a result it enforces the single responsiblity principle.

Email and logging class are re-useable. You can inject them in any other modules where they are needed.


In this blog, we discussed SRP which is a very important principle in today’s programming world. It makes code modular, leaner, and testable. In the next blog, we will discuss the Interface Segregation Principle and its importance.