πŸš€ SOLID Principles Series – Part 1: Single Responsibility Principle (SRP)

Kawal JainKawal Jain
3 min read

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

0
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