Skip to content

Software principles for non-software developers – Single-responsibility-principle

single-responsibility principle

The story is about a man, called Bai Ivan. Bai Ivan lives in Sofia but spends a lot of time in a nearby village, where he owns a small house. This house is an endless project for Bai Ivan and he is always looking to improve something there – a new terrace, a place to park the car, a bench, a playground for the grandchildren.

Everything was fine until the day he wanted to put a fan into the bathroom. It makes a lot of sense to put a fan there. The bathroom is often wet and you need to open a window to fix that. But sometimes it’s cold, or too hot, or you do not want any insects to go in, so it’s better to have a fan. Bai Ivan bought a fan, installed it, connected it to electricity, connected it to the light switch. Bai Ivan finished another small project.

If you put the lights on, it will also switch on the fan.

So far, so good.

It happens that I am also a part of that story. It happens that Bai Ivan wanted to change something in the fan – maybe it got broken, I do not remember. So he started to work and here comes my important part of the story. I saw him working, in the dark, so wanting to help, switched on the lights.

Bai Ivan was fine after that, not even shaken 🙂 The story ends with a happy ending – the fan fixed.

The moral of the story

The light-switch was responsible both for the lights and the fan. Instead of a single responsibility, it got a double one.

This made making changes to the fan connected to the changes in the lights. Because these two were now coupled, this whole system had more than one reason to change

So we have arrived at something close to the definition of Single-responsibility principle (SRP)

A class should have only one reason to change

Robert C Martin aka Uncle Bob

I have used this and a few other stories for years now when I tried to explain important software principles to people – programmers or not. I have also made a talk about this before.

I guess it was time to share at least part of this here as well!

Thank you for reading!

Leave a Reply

Your email address will not be published.