π SOLID Principles Series β Part 1: Single Responsibility Principle (SRP)


There is no better way to start off my post than introducing you to the SOLID Principles β a set of five rules that every developer, regardless of experience, needs to master in order to produce clean, scalable, performance-oriented, and easy-to-maintain code. With this in mind, allow me to introduce to you the S of SOLID: The Single Responsibility Principle (SRP).
Single Responsibility Principle (SRP)
The Single Responsibility Principle states:
βA class should have only one reason to change.β β Robert C. Martin
A class should only do one task and, ideally, have one responsibility. A class that consists of more than one responsibility has the potential to become coupled with multiple system aspects. This would mean that when a single responsibility changes, unexpected and unwanted bugs could arise.
The Problem with Multiple Responsibilities
Imagine you're working with the User class in JavaScript:
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
validateEmail() {
// Validate email format
}
saveToDatabase() {
// Logic to save user to DB
}
sendWelcomeEmail() {
// Email logic
}
}
This User Class Handler does too much responsibilities:
It Validates user data
It Handles persistence
It Sends emails
Consider a scenario where you want to change the logic of email-sending β you have to change the User class which is unrelated to user data. This in itself violates SRP.
Refactoring Using SRP
Letβs break responsibilities into focused classes:
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
}
class UserValidator {
static validate(user) {
// Check if user.email is valid
}
}
class UserRepository {
static save(user) {
// Save to DB
}
}
class EmailService {
static sendWelcomeEmail(user) {
// Send email
}
}
Each class now has only one reason to change:
UserValidator
changes only if validation logic changes.UserRepository
changes only if storage changes.EmailService
changes only if email logic changes.
This is SRP in action!
Why SRP Matters
Maintaining a system SRP compliant is bound to improve:
β Better Maintainability β Less change will improve unrelated broken functionality.
β Improved Readability β Focused and clear cut code will be far easier to read.
β Simpler Testing β Mocking and unit testing class is made easier by small class size.
β Enhanced Reusability β Independent use of each class without affecting the other improves reusability.
Key Takeaways
One class = One responsibility.
SRP makes your code cleaner, modular, and more testable.
Whatβs Next?
In the following sections of the series we will discuss the Open/Closed Principle, which teaches us how to design our code in a way that allows for easy extensibility but restricts alteration of existing code.
Follow along and stay tuned!
I hope you enjoyed this..! Feel free to share your thoughts, questions, or ideas for future topics in the comments.
Thanks for reading
Happy coding!
If you enjoyed the article and would like to show your support, be sure to:
Follow me On Medium
Follow me On Hashnode
Checkout more Portfolio
Subscribe to my newsletter
Read articles from Kawal Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kawal Jain
Kawal Jain
π§βπ» Full Stack Developer | JavaScript & GenAI Enthusiast π 7+ years building scalable web apps with React, Node.js & PHP π‘ Exploring GenAI, Microservices, and Clean Code Principles π Blogging my dev journey, lessons learned, and side experiments βοΈ Chess lover | Vegetarian foodie | Life-long learner