Single Responsibility Principle

AHMET ALTIOGLU
5 min readFeb 6, 2021

--

Hi guys. It has been almost a month since I published my last story. And, I’m not gonna waste your time reading how I spent my last month. Basically, I had exams, projects to finish and things to prevent me from writing a story. And, finally here We are!

I have thought of a series on SOLID Principles. And You guys must have seen it on title that we will cover Single Responsibility Principle at first. Speaking of which, SRP(Single Responsibility Principle) is, i believe the easiest one to fully understand among those principles. And, I will try to cover SRP by referring to a project which means that I will show you both the logic and a use-case scenario so that you will get it.

To start with SRP, You must first know a little bit of SOLID Principles. Basically, SOLID is an acronym for five design principles; meaning each word has a concept. It was prometed by an American Software Engineer Robert C. Martin who has a great influence

on programmers. Most of the people think SOLID is an requirement. Indeed, it is not a requirement but just principles that help us write clean, reusable, convenient, and sustaniable code.

Now, we can start covering our topic: Single Responsibility Principle(SRP). When you are writing code, are you doing it right? How do you know the bad code? Why do we write code that slows us down? These are the very first questions -at least Uncle Bob asks those questions in this link- that you might ask yourself when it comes to learn one of the Solid Principles. I already asked myself questions above and ended up talking about them here :). So, I suggest you to do the same so that it will get stuck in your memory and you will never free it up(if you don’t have a GC attached in you brain lol). SRP(Single Responsibility Principle) is explained as follows:” A class should have one, and only one reason to change.” Makes sense? If it doesn’t now, then it will; if you follow me along the way. Before showing how the code is supposed to look with SRP, i will at first declare a bunch of statements about it and later on we will look at how SRP solves those statements.

1- Code has to be easy to understand, read and debug.

2- When we have to make changes, we have to know where to look.

3- The more complexity, the more bad code(not always but might be).

Imagine, you make an app with which you can generate a random nickname out of your real name. Your app’s Main Method might be looking like this:

Read this from up to the all the way down
Read this from up to all the way down

Now, sit tight! It is time to ask and answer some questions. The code above, doesn’t it look so bad and messy? There are so much bad and meaningless methods appearing eveywhere which might prevent bad code. Now, I want you to remember my statements. Code above violates my first statement, which means it isn’t easy to understand, read and debug. What about making new changes? I am hell sure I won’t be able to find a programmer who wants to change someting in this code. Because it looks like it came in the belly of the beast. And, who would like to deal with that? No one (If you think that you can make changes without going crazy, then let me know in comment below).

Now, in order to apply SRP(Single Responsibility Principle) in code, I am gonna go ahead and change the way code stands and add a few things so that you will see the things we should avoid and the things we should consider.

Main Method of the App

Realize that I just added 2 simple classes as you can see on top-left with the plus symbol. And, both of those classes have the same purpose in life, which is; they both do just one thing. And, for the sake of the insanity if you want to make new changes in code, now, it is so easy to find your way right?. Because, if i wanna change the welcome message, i know where to look. And, with one change in Message Class will change every usage of that. On the other hand, I am hell sure that now i might find a programmer who wants to make changes in code. So, the key is simple when you want to use SRP Concept in your code. Write code in a way that every class, method, etc. only does one thing. And, make sure there’s only one reason your each class to be changed.

Before you guys tell me in comment below, i better show you how other classes look. After this we will wrap up this story.

CustomerManager Class
My Suber-Uber-Awesome App’s UI
My name looks meaningfull doesn’t it?
Look at the nickname. Looks like I need to change the algorithm this app uses when generating a nickname.

For the sake of this example, I decided not to reveal the lines in methods. Because i think there’s no point to do that(don’t think that methods are empty. To shape things in my mind I added lines according to the method’s needs).

So, it looks like we are done in this story. But, remember that there’s only one way to learn something literally. It is: “Doing that thing”. So, I higly suggest you guys to read, watch, think and write code on your own. To prove that, after i learnt SRP, I decided to write a story about it so that my learning curve will rise up. And, last but not least, we can’t write clean code with only SRP Concept but with combining all those 5 principles together. So, stay tuned, because there will be more :).

--

--

AHMET ALTIOGLU
AHMET ALTIOGLU

Written by AHMET ALTIOGLU

I am a motivated Software Developer.

No responses yet