Using Single Responsibility to write amazing code [Technique Tuesday]
This will help you far beyond the Coding Interviews
Happy Tuesday my amazing readers,
I hope you’re grooving your way through life. Today, I will be covering a very important principle in Software Engineering. The idea we will be discussing today is called the Single Responsibility Principle. It is something that will help you both in Coding Interviews and as you build your career in Software Engineering. Following this principle allowed me to write clean code that was easy to understand. This is feedback from a Senior Developer reviewing scripts I created
When it comes to solving harder coding interview problems, practicing this will be extremely helpful. In this email, I’ll first introduce the concept, and then tell you how to use it in your interviews. Let’s get right into it.
The Single Responsibility Principle
I’m sure it sounds very intimidating. But the idea behind this principle is simple. As the name indicates, it states that all classes and modules should have only 1 well-defined responsibility. Robert C Martin, the biggest promoter of these principles states the principle as
A class should have one, and only one reason to change.
This means when we design our classes, we need to ensure that our class is responsible only for 1 task or functionality and when there is a change in that task/functionality, only then, should we change that class.
Implementing the Single Responsibility principle requires taking a birds-eye view of the problem. However, implementing this principle has more benefits than just readability. This principle can help with-
Your Code is much easier to change. Instead of having a to wade through a large codebase, you only need to worry about the specific area you’re testing.
Debugging is so much simpler. Single Responsibility allows you to quickly pinpoint which aspect of your solution is missing out.
Writing Tests is a breeze since the testing can be a lot more specific.
More people can get in simultaneously on the project. Furthermore, people can focus on the areas that they are good at, without worrying about the other aspects. I hate SQL. In a good ML pipeline, I will be told to work on the Data Analysis portion, and a SQL-loving weirdo can handle the Data Querying and Joins.
You can see why the Single Responsibility Principle is part of SOLID. Implementing it will give you amazing performance reviews and rocket-boost your career. However, what does it have to do with Coding Interviews? After all, most coding interviews are not take-home projects. Let’s do that next.
Single Responsibility in Coding Interviews
To get the low hanging fruit out of the way, most big companies have multiple rounds of interviews. Among these rounds will be Code Reviews, where understanding this principle will be key. The aforementioned Take Home Project will be another area where this principle helps. It also goes hand in hand with OOP.
But most of you want help with Leetcode-Style Coding Questions. By their very nature, these problems are self-contained and won’t be spanning multiple functionalities. Can Single Responsibility help us here?
Above is my 5 Point Approach to acing all interviews. Those who have seen the FAQs will be familiar with this table. Why is that relevant?
The idea behind Single Responsibility is to break down a complex system into simpler components, where each component has a narrowly defined focus. It is a very direct way of promoting modularity in your solutions.
Modularity is also very important in Leetcode Style Questions. Instead of classes, we need modularity in our functions and ideas. We need to develop in a step-by-step and organized manner. Good scores in communication and problem solving require this.
However, Single Responsibility in these questions will look slightly different. Instead of analyzing the system as a whole, we instead target one specific area. We use the information given to us to solve a small sub-problem. Once we solve the sub-problem, we use the new information given to us to solve another portion. Piece by piece, we build out our solution, by focusing on one single element at a time.
This is also a kind of modularity. Getting into the habit of breaking down the whole problem into a series of related sub-portions and solving them will allow you to tackle any problem. Harder problems are hard because of the number of moving parts they have. Once you can start isolating different moving parts based on the information you have, your performance will skyrocket.
Before proceeding, if you have enjoyed this post so far, please make sure you like it (the little heart button in the email/post). I also have a special request for you.
***Special Request***
This newsletter has received a lot of love. If you haven’t already, I would really appreciate it if you could take 5 seconds to let Substack know that they should feature this publication on their pages. This will allow more people to see the newsletter.
There is a simple form in Substack that you can fill up for it. Here it is. Thank you.
https://docs.google.com/forms/d/e/1FAIpQLScs-yyToUvWUXIUuIfxz17dmZfzpNp5g7Gw7JUgzbFEhSxsvw/viewform
To get your Substack URL, follow the following steps-
Open - https://substack.com/
If you haven’t already, log in with your email.
In the top right corner, you will see your icon. Click on it. You will see the drop-down. Click on your name/profile. That will show you the link.
You will be redirected to your URL. Please put that in to the survey. Appreciate your help.
In the comments below, share what topic you want to focus on next. I’d be interested in learning and will cover them. To learn more about the newsletter, check our detailed About Page + FAQs
If you liked this post, make sure you fill out this survey. It’s anonymous and will take 2 minutes of your time. It will help me understand you better, allowing for better content.
https://forms.gle/XfTXSjnC8W2wR9qT9
Happy Prep. I’ll see you at your dream job.
Good things take time,
Devansh <3
To make sure you get the most out of Technique Tuesdays, make sure you’re checking in the rest of the days as well. Leverage all the techniques I have discovered through my successful tutoring to easily succeed in your interviews and save your time and energy by joining the premium subscribers down below. Get a discount (for a whole year) using the button below
Reach out to me on:
Instagram: https://www.instagram.com/iseethings404/
Message me on Twitter: https://twitter.com/Machine01776819
My LinkedIn: https://www.linkedin.com/in/devansh-devansh-516004168/
My content:
Read my articles: https://rb.gy/zn1aiu
My YouTube: https://rb.gy/88iwdd
Get a free stock on Robinhood. No risk to you, so not using the link is losing free money: https://join.robinhood.com/fnud75