Introduction
In this article, you will learn about the Single Responsibility Principle.
Single Responsibility Principle (SRP)
- The Single Responsibility Principle states that a class should have one and only one reason for the change.
- The benefits of SRP include:
- Reduction in complexity of code
- Increased readability, extensibility, and maintenance
- Reusability and reduced errors
- Better testability
- The classes and interfaces in an application often become bloated when they have to perform too many different responsibilities.
- The basic idea behind SRP is that two or more separate responsibilities should be separated into distinct classes or modules - it is a bad design approach to couple different responsibilities in the same class if such responsibilities might change over time
- The class should have one purpose, if you want to change or modify the class then it should be in one area(class).
- Class size should not be more than 40-50 lines. If the screen is getting a scrollbar when you open your class then we need to think about how can we divide the class into different classes.
Consider the below example:
- namespace ConsoleUI
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Welcome to the Application");
- //Ask for Information
- Person user = new Person();
- Console.WriteLine("What is your First Name");
- user.FirstName = Console.Readline();
- Console.WriteLine("What is your Last Name");
- user.LastName = Console.Readline();
- //Ask for Validation
- if(String.IsNullOrWhiteSpace(user.FirstName))
- {
- Console.WriteLine("You didnt have valid First Name");
- Console.Readline();
- return;
- }
- if(String.IsNullOrWhiteSpace(user.LastName))
- {
- Console.WriteLine("You didnt have valid Last Name");
- Console.Readline();
- return;
- }
- //Final output to User
- Console.WriteLine($"Your UserName is {user.FirstName}{user.LastName}");
- Console.Readline();
- }
- }
- }
What problem we can figure out from the above program,
- When the project contains 50 lines of code and the developer wants to change the code as per the new requirement, it can be managed. However, consider that if in 6 months when the code exceeds 500 lines and all lines are doing different-different stuff, how the developer is going to manage everything?
- A new requirement comes. Instead of “What is your First Name” it should be “What is your First Name Sir/Madam”. With these small lines of code, it is manageable. But consider a scenario,(after 6 months) code has 5 sections- (Name, Age, Gender, Education, Family background ). The developer needs to figure out from where we are taking this information in code.
- Merging the code if multiple developers are working at the same time for different module
- The problem for the long run will be:
- It will lead to the time-consuming task as code complexity increases
- The risk of breaking the code is more. Even for small change, we need to test the whole project
- Maintaining the application is difficult
Solution
It can vary from developer to developer:

In the above program, we can identify 5 different sections that are independent of each other:
- Everything related to Printing output:
- Some are welcome message
- Some are requesting information message
- Some are validation message
- Some are Final Output message
- Hold the Screen
- Creating User and requesting information
- Validation
- Perform the Final output process
Let's create a module that will show all messages to Users.
In the future, if a user logs in to any module, we need to show a welcome message. In this case, we will refer to the class.
- public class WelcomeMessage
- {
- public static void DisplayWelcomeMessage()
- {
- Console.WriteLine("Welcome to the Application");
- }
- }
Join the conversation! Your thoughts help the community grow.