"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > SOLID: S - Single Responsibility Principle (SRP)

SOLID: S - Single Responsibility Principle (SRP)

Published on 2024-08-20
Browse:421

SOLID: S - Single Responsibility Principle (SRP)

Introduction to SRP:
The Single Responsibility Principle (SRP) is one of the five SOLID principles, a set of guidelines for writing cleaner and more sustainable code. SRP states that a class should have only one reason to change, meaning it should have only one responsibility or function. Following this principle makes the code easier to understand, maintain, and test.

Objectives of SRP:

  • Simplified Maintenance: With classes having only one responsibility, identifying and fixing bugs becomes easier.
  • Clear Responsibility: Each class has a clear purpose, making the code easier to understand.
  • Improved Testability: Classes with single responsibilities are easier to isolate and test.
  • Ease of Change: Changes in a specific responsibility do not affect other parts of the system.

Bad Practice Example (Classes):
Here we have a UserService class that does more than one thing: manages users and sends notifications.

class UserService {
  createUser(user: User): void {
    // Logic to create user
  }

  deleteUser(userId: string): void {
    // Logic to delete user
  }

  notifyUser(userId: string, message: string): void {
    // Logic to notify user
  }
}

In this approach, the UserService class has multiple responsibilities: managing users and sending notifications. This violates SRP.

Good Practice Example (Classes):
To apply SRP, we can separate the responsibilities into distinct classes.

class UserService {
  createUser(user: User): void {
    // Logic to create user
  }

  deleteUser(userId: string): void {
    // Logic to delete user
  }
}

class NotificationService {
  notifyUser(userId: string, message: string): void {
    // Logic to notify user
  }
}

Now, UserService handles only user creation and deletion, while NotificationService handles notifications. Each class has a single responsibility, following SRP.

Bad Practice Example (Functions):
Here we have a function that does more than one thing: creates a user and sends a notification.

function createUserAndNotify(user: User, message: string): void {
  // Logic to create user
  // Logic to send notification
}

In this approach, the createUserAndNotify function has multiple responsibilities: creating a user and sending a notification. This violates SRP.

Good Practice Example (Functions):
To apply SRP, we can separate the responsibilities into distinct functions.

function createUser(user: User): void {
  // Logic to create user
}

function notifyUser(userId: string, message: string): void {
  // Logic to notify user
}

// Using the separated functions
createUser(newUser);
notifyUser(newUser.id, 'Welcome!');

Now, the createUser function handles only user creation, while notifyUser handles notifications. Each function has a single responsibility, following SRP.

Application in React Native with TypeScript:
Imagine we are developing a task management app. We can apply SRP by separating task management logic and notification logic into different classes.

Bad Practice Example (Classes):

class TaskService {
  addTask(task: Task): void {
    // Logic to add task
  }

  removeTask(taskId: string): void {
    // Logic to remove task
  }

  notifyTaskDue(taskId: string): void {
    // Logic to notify that the task is due
  }
}

Good Practice Example (Classes):

class TaskService {
  addTask(task: Task): void {
    // Logic to add task
  }

  removeTask(taskId: string): void {
    // Logic to remove task
  }
}

class TaskNotificationService {
  notifyTaskDue(taskId: string): void {
    // Logic to notify that the task is due
  }
}

Bad Practice Example (Functions):

function addTaskAndNotify(task: Task): void {
  // Logic to add task
  // Logic to notify that the task is due
}

Good Practice Example (Functions):

function addTask(task: Task): void {
  // Logic to add task
}

function notifyTaskDue(taskId: string): void {
  // Logic to notify that the task is due
}

// Using the separated functions
addTask(newTask);
notifyTaskDue(newTask.id);

By dividing responsibilities, we make the application easier to maintain and expand.

Conclusion:
Following the Single Responsibility Principle helps keep the code clean, organized, and easier to maintain. Applying SRP in React Native development with TypeScript results in more modular and testable code. Always remember to keep your classes and functions focused on a single responsibility to reap all the benefits of this principle.

Release Statement This article is reproduced at: https://dev.to/paulocappa/solid-day-1-s-single-responsibility-principle-srp-1f5k?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3