Single responsibility principle is the S in SOLID.
There is a proverb present in many languages.
Many chefs spoil the soup.
In German there is a version:
Many chefs spoil the porridge, by adding too much salt.
(It sounds much better in German.)
I like the German version since it is specific on how the porridge is ruined. Each chef has their own plan, adding their own salt, and the porridge can’t take it. That is what happens with code as well. The single responsibility principle is about that.
Code should have only one responsibility, so that there is only one chef who might put salt in it. If a piece of code has more than one stakeholder, chef, too much stuff, salt, will be added to the code.
So what is a chef/stakeholder in this case?
Stakeholders are people, or organisations, who want to change how the code works.
It can be an external API that is used, and then changes. Governments can change a law that effects your code. Marketing might be a stakeholder. A specific target audience for the product might add extra salt.
By separating all those interests, preferable using interfaces, we can make sure that each chef only puts salt in their own pot of porridge.
ps. Developers sharing responsibility for code is a good thing. We are not stakeholders in this example, as long as we work as a team.
Comment on the blog!
Create a comment by emailing me.
I will then add the comment to the post.