The Single Responsibility Principle: Enhancing Code Maintainability

Kittipat.Po
7 min readMay 26, 2024

--

This article is part of a series that explores the SOLID principles of object-oriented design, providing insights and practical examples to help you write better code. The content and principles discussed are derived from the book “Clean Architecture” by Robert C. Martin, offering guidance on creating maintainable, scalable, and robust software systems.

Table of Contents

  1. The Single Responsibility Principle
  2. The Open-Closed Principle
  3. The Liskov Substitution Principle 🚧
  4. The Interface Segregation Principle 🚧
  5. The Dependency Inversion Principle 🚧

Introduction

The Single Responsibility Principle (SRP) is one of the five SOLID principles of object-oriented design. SRP asserts that a class should have only one reason to change, meaning it should only have one job or responsibility. Understanding SRP is crucial for creating maintainable and scalable software systems. In this blog, we will dive into the SRP, explore its importance, and illustrate how to apply it effectively with practical examples.

What is the Single Responsibility Principle?

The Single Responsibility Principle (SRP) is one of the core principles of object-oriented design.

It states:

A module should have one, and only one, reason to change.

This principle emphasizes that a class should only have one job or responsibility. Each class should be focused on a single task or function, which makes the system easier to maintain and extend. However, SRP is often misunderstood due to its name. It’s not just about doing one thing, but about having a single reason to change.

Historically, the SRP has been described as:

A module should have one, and only one, reason to change.

Software systems are changed to satisfy users and stakeholders; those users and stakeholders are the “reason to change” that the principle is talking about.

However, a better way to phrase this is:

A module should be responsible to one, and only one, actor.

An actor here refers to a group of stakeholders or users with the same reasons to request changes. Understanding this distinction is crucial to applying SRP effectively.

Example of SRP Violation:

Symptom 1: Accidental Duplication

For instance, in a payroll system, different departments may have different reasons for changing the system.

  • Accounting might need to change how pay is calculated.
  • HR might need changes to reporting hours.
  • DBAs might need to change how data is stored.

Each of these reasons should be isolated to separate modules.

Consider an example from a payroll application with an Employee struct that has three methods: calculatePay(), reportHours(), and save().

type Employee struct {
ID int
Name string
PayRate float64
HoursWorked float64
}

func (e *Employee) CalculatePay() float64 {
// Code to calculates the total pay for the employee
}

func (e *Employee) ReportHours() string {
// Code to summarizing the hours worked by the employee
}

func (e *Employee) Save() {
// Code to save employee to database
}

This struct violates the Single Responsibility Principle (SRP) because these three methods are responsible for tasks important to three different departments:

  • CalculatePay(): Used by the accounting department, which reports to the CFO.
  • ReportHours(): Used by the human resources department, which reports to the COO.
  • Save(): Used by the database administrators (DBAs), who report to the CTO.

By combining these methods in a single Employee struct, developers have coupled the interests of the CFO, COO, and CTO. This coupling can lead to unintended consequences when changes are made.

Example of the Problem

Suppose both calculatePay() and reportHours() share a common algorithm for calculating non-overtime hours. To avoid code duplication, developers extract this logic into a function called regularHours().

func (e *Employee) regularHours() float64 {
if e.HoursWorked > 40 {
return 40
}
return e.HoursWorked
}

func (e *Employee) CalculatePay() float64 {
return e.PayRate * e.regularHours()
}

func (e *Employee) ReportHours() string {
return fmt.Sprintf("%s worked %.2f regular hours", e.Name, e.regularHours())
}

Now, suppose the CFO’s team decides that the calculation of non-overtime hours needs to be tweaked. However, the COO’s team in HR does not want this change because they use non-overtime hours for a different purpose.

A developer updates the regularHours() function to meet the CFO's requirements:

func (e *Employee) regularHours() float64 {
if e.HoursWorked > 40 {
return 35 // Adjusted based on new accounting rules
}
return e.HoursWorked
}

The developer tests the change, and the CFO’s team confirms it works as desired. The system is deployed without notifying the HR team. As a result, the reportHours() function now produces incorrect data, as it relies on the regularHours() function that has been altered for a different purpose.

Symptom 2: Merges

Merges are a common issue in source files that contain multiple methods, especially when these methods serve different actors. Let’s look at a simplified example to understand this problem better.

Consider our earlier Employee struct in a payroll application. This struct has methods to calculate pay, report hours, and save employee data

type Employee struct {
ID int
Name string
PayRate float64
HoursWorked float64
}

func (e *Employee) CalculatePay() float64 {
// Code to calculates the total pay for the employee
}

func (e *Employee) ReportHours() string {
// Code to summarizing the hours worked by the employee
}

func (e *Employee) Save() {
// Code to save employee to database
}

The Problem with Merges

Imagine the following scenario:

  1. The CTO’s team of DBAs decides to make a simple schema change to the Employee table in the database.
  2. At the same time, the COO’s team of HR clerks decides to change the format of the hours report.

Two different developers from these teams check out the Employee struct and start making changes:

  • The DBA developer modifies the Save() method to handle the new schema.
  • The HR developer changes the ReportHours() method to meet the new report format requirements.

When both developers try to merge their changes back into the main codebase, a merge conflict occurs. The tools we use for merging code are quite advanced, but they can’t handle every scenario perfectly. There’s always a risk that the merge will introduce bugs or cause unexpected behavior.

Challenges with Complex Responsibilities

Beyond accidental duplication and merge conflicts, having multiple responsibilities within a single class introduces several other challenges. These complexities can further complicate the system, making it harder to manage and maintain. Here are some common challenges:

  • Complex Testing: When a class has multiple responsibilities, testing it becomes more complex and cumbersome. Each method in the class might require different setups, dependencies, and conditions, making unit tests harder to write and maintain.
  • Complex Dependencies: When a class has multiple responsibilities, it often ends up with numerous dependencies. These dependencies can make the class difficult to manage, as changes in one responsibility can affect the others, leading to a fragile system.

Solution

Separate Data from Functions

The most straightforward approach is to separate the data from the functions. Here’s how we can do this:

  1. EmployeeData: A simple data structure with no methods.
  2. PayCalculator: Responsible for calculating pay.
  3. HoursReporter: Responsible for reporting hours.
  4. EmployeeSaver: Responsible for saving employee data.
type EmployeeData struct {
ID int
Name string
PayRate float64
HoursWorked float64
}

type PayCalculator struct{}

func (pc *PayCalculator) CalculatePay(e *EmployeeData) float64 {
// Code to calculates the total pay for the employee
}

type HoursReporter struct{}

func (hr *HoursReporter) ReportHours(e *EmployeeData) string {
// Code to summarizing the hours worked by the employee
}

type EmployeeSaver struct{}

func (es *EmployeeSaver) Save(e *EmployeeData) {
// Code to save employee to database
}

In this setup, each class holds only the source code necessary for its particular function. The three classes do not need to know about each other, thus avoiding any accidental duplication.

Using the Facade Pattern

A common solution to manage the multiple classes is to use the Facade pattern. The EmployeeFacade class will contain minimal code and will be responsible for instantiating and delegating tasks to the appropriate classes.

type EmployeeFacade struct {
payCalculator PayCalculator
hoursReporter HoursReporter
employeeSaver EmployeeSaver
}

func (ef *EmployeeFacade) CalculatePay(e *EmployeeData) float64 {
return ef.payCalculator.CalculatePay(e)
}

func (ef *EmployeeFacade) ReportHours(e *EmployeeData) string {
return ef.hoursReporter.ReportHours(e)
}

func (ef *EmployeeFacade) Save(e *EmployeeData) {
ef.employeeSaver.Save(e)
}

Keeping Business Rules Close to Data

Some developers prefer to keep the most critical business rules close to the data. This can be done by maintaining the essential method in the original Employee class and using that class as a Facade for the other functions.


type Employee struct {
Data EmployeeData
PayCalculator PayCalculator
HoursReporter HoursReporter
EmployeeSaver EmployeeSaver
}

func (e *Employee) CalculatePay() float64 {
return e.PayCalculator.CalculatePay(&e.Data)
}

func (e *Employee) ReportHours() string {
return e.HoursReporter.ReportHours(&e.Data)
}

func (e *Employee) Save() {
e.EmployeeSaver.Save(&e.Data)
}

Additional Considerations

Benefits of SRP

  1. Improved Maintainability: When each class has a single responsibility, it becomes easier to understand, test, and maintain.
  2. Enhanced Reusability: Classes with a single responsibility can often be reused in different contexts or applications.
  3. Simplified Debugging: Issues are easier to trace when they arise from a class with a single responsibility.

Practical Tips

  1. Identify Responsibilities Early: During the design phase, clearly identify and separate responsibilities.
  2. Refactor Regularly: As requirements evolve, continually refactor your classes to ensure they adhere to SRP.
  3. Code Reviews and Pair Programming: Regular code reviews and pair programming can help catch SRP violations early. Having another set of eyes on your code can provide different perspectives on how responsibilities can be better separated.

Conclusion

The Single Responsibility Principle (SRP) is fundamental to writing maintainable and scalable software. By ensuring that each class has only one reason to change, you reduce the risk of unintended side effects, simplify debugging, and make your codebase more understandable.

SRP not only applies to functions and classes but also at higher levels, evolving into principles like the Common Closure Principle at the component level and influencing architectural boundaries. As you continue to design and refactor your software, keep SRP in mind to build robust, maintainable systems.

--

--