Understanding Single Responsibility Principles: Real-World Analogy

Fawwaz Naabigh
3 min readMay 21, 2024
Photo by Quality Pixels on Unsplash

Yesterday, I was challenged to explain Single Responsibility Principle (SRP) in Object-oriented Programming (OOP) to non-programmers. Se let’s begin.

What is SRP?

SRP is the first principle of SOLID design principles in OOP. The complete list of SOLID design principles are:

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

The core idea of SRP is to ensure that every class, function, or module in a program only has one purpose/responsibility and only one reason to change.

But, if you are a non-programmer, I am sure that you didn’t understand the definition mentioned before. So, let’s use an analogy to understand it better.

Let’s say you have a well-organized toolbox that have complete tools that each have their own specific jobs. Following the SRP principle, each tool should have only one purpose. The hammer for driving nails, the screwdriver for installing screws, and the saw for cutting wood.

Following the principle, you could do your job efficiently. What happens if you violate the principle? What if a screwdriver responsibilities are installing screws and driving nails. Sure, you probably could still do your job, but it will be more inefficient. Imagine the effort you should do for driving nails using screwdriver.

More over, new craftworkers would be confused in their first day they worked with you. The time they want to drive a nail and read your manual book about the purpose of each tools, they will be unsure which tools should be used.

Why SRP is Important?

SRP has several importances in software development.

  1. Easy to understand. By following the SRP principle, reading and understanding the code becomes easier because each class, function, or module only has one responsibility or purpose.
  2. Easy to change. SRP forced the programmer to break down complex functionality, module, or class into smaller and more focused modules. By doing this, changes can be made easily without affecting other parts or introducing unintended side effects.
  3. Easy to reuse. Generally, code that follows SRP will be more modular and reusable. Programmer could easily use a module in other part of the program.

Overall, SRP increases the maintainability of a program by making it easier to understand, to change, and to reuse.

Conclusion

SRP is a principle that says a function, module, or class should have only one purpose and only one reason to change. By keeping the code more focused on one responsibility, it offers some benefits including easy to understand, easy to change, and easy to reuse.

Resources

--

--