Clean code
  • Overview
    • Introduction
    • Why is clean code so important?
    • What Is Clean Code?
    • How to write clean code?
    • Conventions
  • The key principles of clean code
    • Meaningful Names
    • Functions
    • Classes
    • Comments
    • Error Handling
      • Exception handling best practices
    • Unit Tests
    • Formatting
    • Objects and Data Structures
    • Simple Design Rules
    • Concurrency
    • Code Smells
  • Building Maintainable Software
    • Write Short Units of Code
    • Write Simple Units of Code
    • Write Code Once
    • Keep Unit Interfaces Small
    • Write Clean Code
    • Automate Tests
  • Bonus
    • SOLID Principle
      • SRP - Single Responsibility Principle
      • OCP - Open-Closed Principle
      • LSP - Liskov Substitution Principle
      • ISP - Interface Segregation Principle
      • DIP - Dependency Inversion Principle
    • LoD Principle
    • YAGNI Principle
    • DRY Principle
    • Fail Fast principle
    • Hollywood Principle
    • Library vs Framework
    • Coupling and Cohesion
    • AOP - Aspect-Oriented Programming
      • Building an AOP framework
    • OOP Design Pattern
    • Technical Dept
    • How to learn software Design and Architecture - Roadmap
    • Microservcies
      • Defining the scope of a microservice
      • Step-by-Step: How to Identify Over-Scoped Microservices
      • Benefits of Grouping or Consolidating Microservices
      • A practical step-by-step plan to consolidate microservice
Powered by GitBook
On this page
  • Example
  1. Bonus
  2. SOLID Principle

SRP - Single Responsibility Principle

A class should have one and only one reason to change, meaning that a class should have only one job.

PreviousSOLID PrincipleNextOCP - Open-Closed Principle

Last updated 2 years ago

The Single Responsibility Principle (SRP) states that a class or module should have one, and only one reason to change. This principle gives us both a definition of responsibility and guidelines for class size. Classes should have one responsibility—one reason to change.

According to this principle, we have to separate this class into many separate classes, each of which has only one responsibility. Although more classes will be generated, it is easier to understand, easy to reuse, and smaller classes so fewer bugs.

Some examples of SRPs to consider that can be segregated include Persistence, Validation, Messages, Error Handling, Logging, Formatting, Parsing, and Mapping, ...

is how various parts of a software component are related. Higher cohesion helps to achieve better adherence to the Single Responsibility Principle.

Example

To understand this principle, let's see an example of a class that violates this principle as follows:

class UserService {
    public User createUser(UserDto user) {
        // INSERT INTO user(...);
    }

    public boolean isStrongPassword(String password) {
        // Matcher.match("Strong password Regex pattern");
    }

    public void sendEmail(String toEmail, String content) {
        // Get email config
        // Create Gmail object
        // Send email
    }

    public void log(String message) {
        // Console.log(....);
    }
}

As you can see, this class performs a lot of different responsibilities: creating use, validation, sending emails, logging, etc. When we just need to change the way we create the user, get the data from DB, or change the authentication way… we will have to modify this class, which has to get bigger and bigger in the future. Very difficult to maintain, upgrade, fix, test, etc.

According to this principle, we must separate this class into many separate classes, each of which only does a single task, e.g. UserRepository, PasswordValidator, EmailSender, Logger,….

class UserRepository {
    public User createUser(UserDto user) {
        // INSERT INTO user(...);
    }
}

class PasswordValidator {
    public boolean isStrongPassword(String password) {
        // Matcher.match("Strong password Regex pattern");
    }
}

class EmailSender {
    public void sendEmail(String toEmail, String content) {
        // Get email config
        // Create Gmail object
        // Send email
    }
}

class Logger {
    public void log(String message) {
        // Console.log(....);
    }
}

Cohesion