Fix Your Code Using The Single-Responsibility Principle

‌‌‌‌‌‌
codeburst
Published in
4 min readJul 31, 2020

--

Photo by JESHOOTS.COM on Unsplash

The symptoms are mild at first, but a lack of awareness and ability to contain it immediately causes a rapid spread. No, I’m not talking about COVID-19, I’m talking about disorganized code. If it isn’t fixed it right away, it’ll affect everything after it. Luckily, convoluted code can be easily prevented by applying the single-responsibility principle.

Here’s what Wikipedia says the Single-responsibility principle means:

The single-responsibility principle (SRP) is a computer-programming principle that states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class, module or function.

TL;DR: Each module, class, or function should do just one thing. In other words, separation of concerns or modularization.

Where The Single-Responsibility Principle Comes In Handy

Admittedly, if you’re writing a simple “Hello World” program, modularizing can be overkill. However, any code that goes deeper than that requires modularizing.

Even though HTML/CSS are not programming languages, it’s a great example to start with. Anyone who’s tried to make a website knows the pain of not modularizing. You try to move a section a couple of pixels to the left and all hell breaks loose. Your website shifts halfway down the screen and your navbar turns purple. Then you cry and reevaluate your life because all of your backend developer friends say that frontend work is easy.

Ok, maybe a bit of an exaggeration.

Source: xkcd

Nonetheless, the main takeaway here is that having a separation of concerns allows for modifying parts of your code without breaking the entire program.

I used to fear writing large programs because I knew that once my program got too large, it would have bugs that would cause more bugs when fixed. If I had just modularized then there wouldn’t have been anything to fear.

Where The Single-Responsibility Principle is Necessary

In general, the single-responsibility principle comes in useful for large applications. Having separate functions for performing individual responsibilities allows for code reusability and easier testing. It is much easier to test a program when you can call the individual functions and see their outputs rather than having to use the debugger to step through each line of the program until you find the bug.

For extremely large applications that may require several developers/teams, modularization is critical. Picture this: you are assigned to work with a different team on a specific feature. The codebase is a decade old, has over 10,000 lines, no documentation, and everything was written in one file. Good luck.

All of this could have been prevented if the programmers who created the mess applied the single-responsibility principle. Even without documentation and properly named variables and functions, simply modularizing can make a codebase significantly more readable.

Don’t be the guy who leaves spaghetti code for an unfortunate new grad/new hire to dig through. Write clean, modularized code. Your co-workers (or graders if you’re in college) will thank you.

Final Thoughts

Aside from general modularizing, another way to think about SRP is by asking yourself the following question: If I change one thing, how many other things will it impact?

As a rule of thumb, limit the impact of change. A useful metaphor to consider is a company like Tesla. While Elon Musk has an understanding of the hardware, assembly, and software for Tesla, he is not the one coding and putting together the cars. Similarly, your main method or app.js shouldn’t be handling all the work. Split it up into different classes and the smaller tasks into different functions. It will make your code easier to test, modify, and read. For more information, there is a great article on the single-responsibility principle by Robert C. Martin, the author of Clean Code.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” — John Woods

For more tips on programming, check out this article I wrote on software engineering essentials

Feeling stressed on LinkedIn while job hunting? Check out LinkedOut, my chrome extension that will boost your productivity and save your mental health when on LinkedIn.

--

--