What Single responsibility (SRP) is?
The Single Responsibility Principle (SRP) is a software design principle that states that a class or module should have only one reason to change. In other words, it should have a single, well-defined responsibility, and should not be responsible for multiple, unrelated tasks.
Why use SRP?
Great, but why is so important to apply SRP you might think, writing one big fat method is simpler, but trust me, it's not the way to go. Instead, breaking down your code into multiple smaller methods can make it easier to understand and modify.
Using multiple smaller methods can help improve code readability and make it easier to debug if something goes wrong. When each method has a clear and well-defined responsibility, it's easier to figure out what the code does without having to search through a large, monolithic method.
On the other hand, a huge method can be tough to read and understand, making it harder to maintain in the long run. By breaking down the code into smaller, well-defined methods, you can avoid these issues and make the code more maintainable over time.
Furthermore, implementing the SRP can result in code that is more readable and understandable. When each class or module has a single responsibility, it is easier to comprehend the purpose of the code and how it fits into the larger system.
Code Example
Now, let's take an example to help you visualize and better understand the concept with some code. After all, we are developers, code is our language and it's often the easiest way to convey ideas.
In the following example, we have an Employee class with its properties and two methods. One to persist an employee into a database and the other to send an email.
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeEmail { get; set; }
public string Department { get; set; }
public void SaveEmployee(Employee employee)
{
// code to save the employee to the database
}
public void SendEmail(string email, string subject, string body)
{
// code to send an email to the employee
}
}
In this example, the Employee
class has two responsibilities: saving the employee data to the database and sending an email to the employee. This violates the SRP because the class should only have one responsibility.
To refactor this to apply the SRP, we could break down this class into two separate classes: EmployeeRepository
and EmailService
.
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeEmail { get; set; }
public string Department { get; set; }
}
public class EmployeeRepository
{
public void SaveEmployee(Employee employee)
{
// code to save the employee to the database
}
}
public class EmailService
{
public void SendEmail(string email, string subject, string body)
{
// code to send an email to the employee
}
}
By splitting the responsibilities between the two classes, we have successfully adhered to the SRP and made the code more comprehensible and sustainable. The Employee class now has a single responsibility, which is to hold the employee's data. The EmployeeRepository class is responsible for persisting the employee data to the database, while the EmailService class is in charge of sending emails.