Single Responsibility Principle in TypeScript

jmalvarez

José Miguel Álvarez Vañó

Posted on August 17, 2022

Single Responsibility Principle in TypeScript

A class should have just one reason to change.

Every class in our code should be responsible for just a single part of the application. By following this principle we reduce the complexity of our code.

If a class is responsible of multiple parts of our app, it will have to be changed frequently. Therefore, changing one part of the class increases the risk of breaking other parts of itself. The solution is to divide it into multiple classes, each one with one responsibility.


In the following bad example we can see how the Student class has two responsibilities: managing the student data and the course data.

class Student {
  id: number;
  name: string;
  courseId: number;
  courseName: string;
  courseSubjects: string[];

  // constructor

  getCourseSubjects(): string {
    return this.courseSubjects.join(", ");
  }
}
Enter fullscreen mode Exit fullscreen mode

Following the Single Responsibility Principle we can improve this by moving the course data to its own class.

class Student {
  id: number;
  name: string;
  course: Course;

  // constructor
}

class Course {
  id: number;
  name: string;
  subjects: string[];

  // constructor

  getCourseSubjects(): string {
    return this.subjects.join(", ");
  }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
jmalvarez
José Miguel Álvarez Vañó

Posted on August 17, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related