DEV Community

Gabrielle Eduarda
Gabrielle Eduarda

Posted on

Single Responsibility Principle: The First Step Toward Sustainable Code

“You might write code that works. But will it still work after someone (or even you) touches it a month from now?”

In the world of software development — especially in .NET — it’s easy to fall into the trap of "just make it work."
The real problem? Code that merely works today can become a nightmare tomorrow if it’s not testable, readable, and ready to evolve.

That’s where the Single Responsibility Principle, or SRP, comes in — the "S" in SOLID.

What is SRP?
A class should have only one reason to change.

That’s the essence of SRP. In other words: a class, service, or module should have one clear responsibility.

It doesn’t mean a class should have only one method or be tiny. It means that everything inside it should serve one coherent purpose.

What happens when we ignore SRP?
You’ve probably seen (or written) something like this:

public class UserService {
public void Register(User user) {
Validate(user);
SaveToDatabase(user);
SendWelcomeEmail(user);
GenerateReport(user);
UpdateCRM(user);
}
}

This class has multiple reasons to change:

  • Validation logic might change
  • Database structure might change
  • Email templates might change
  • CRM integration might change
  • Reporting might change

What starts as a shortcut quickly becomes a change-prone, untestable, unscalable mess.

Applying SRP in .NET
Let’s refactor that code, separating responsibilities:

public class UserRegistration {
private readonly IValidator _validator;
private readonly IUserRepository _repository;
private readonly IEmailService _email;

public void Register(User user) {
    _validator.Validate(user);
    _repository.Save(user);
    _email.SendWelcome(user);
}
Enter fullscreen mode Exit fullscreen mode

}

Now each class has a single, well-defined responsibility.
If the email behavior changes, you only touch EmailService. The rest remains untouched and unaffected.

Real-world benefits of SRP
✔ More testable code
Each responsibility can be tested in isolation, with fewer dependencies.

✔ Safer refactoring
You change only what matters, without fear of side effects.

✔ Better readability
Every class tells a clear story.

✔ Architecture that scales
SRP is the foundation for clean architecture, TDD, and scalable design.

SRP ≠ overengineering
A common mistake: hearing "single responsibility" and creating 20 classes for a login screen.

SRP isn’t about fragmentation — it’s about clarity of purpose.

A class can be 50 lines long and still follow SRP — as long as it has one reason to exist.

How to know if you’re breaking SRP

Use this mental checklist:

  • Does the class have multiple reasons to change?
  • Does it depend on too many external services?
  • Are the unit tests a pain to write or mock?
  • Are you afraid to touch this class?
  • If you answered “yes” to any of those… it’s time to refactor.

Final thoughts: SRP is the gateway
Before diving into DDD, Clean Architecture, or microservices… mastering SRP alone will level up your code quality.

It’s the difference between code only you understand and code your entire team can trust.

Top comments (0)