You might be wondering on the relationship between staying single, getting admired and coding! And, then, staying single forever 🙂 is something which may not be liked by most of us. So, what is this blog all about? Well, with “single, I am trying to point at “Single Responsibility Principle” and with “stay single”, I am pointing towards writing code based on “single responsibility principle”.
Well, as the title of this blog goes, if you keep the “Single” responsibility principle (SRP) in mind while writing your code, there are high chances that you will end up with a great piece of code which will get liked by many including your customers and, you will get admired for your code. This is why I gave the title to this blog as ” Stay Single and Get Admired for Your Code”. Lets see how.
Some of the most common code smells that can have your customers frown are following:
- Long classes
- Long methods
- Long method/API names
Now lets figure out how Single Responsibility Principle can help you avoid above smells from your code.
- Long Classes: Classes get long primarily because one tries to serve more than one functionality from same class. Additionally, the class also lands up containing few utility methods which can be of use to other classes.The way to handle these scenario is to apply “extract class” code refactoring technique. In this technique, one will identify the set of methods/functions which tend to perform similar functionality and extract them out into another class.One simpler way to extract classes from existing long class is to try and define what is it that existing class is trying to achieve. While you are defining, if you come across the “AND” word, you may try the functionality as one and try extracting attributes and behavior/methods related with that functionality into another class.Thus, with “EXTRACT CLASS” refactoring technique, you can make the class shorter by extracting the functionality in another class and extracting the utility methods in another utility class.
- Long Method: Methods get long primarily due to that fact that you may betraying to serve multiple functionality from the same method. Following are some of the ways to identify the long method:
- Using Definition: Try defining method. If you get “AND” joining two functionality, the method may be seen to serve more than one functionality. In this case, extract the functionality separated with “AND” into different method.
- Decision points:Try counting decision points such as if-else/try-catch/for/while/switch. The number is representative of McCabe “cyclomatic complexity” of the method. There may be high possibility that the method may be serving functionality in this scenario. In this case, extract similar decision points/blocks in another method.
- Comments: Try finding block of code with good comment. There are chances that there may be multiple functionality achieve using such block of code. Extract these blocks of code in another method.
- Long Method/API Names: The presence of long method or API name may be an indicator that the method may be trying to achieve multiple functionality using its code. If you find “AND” in the method name, you may want to study the functionality that the method is trying to serve. In that case, extract the block of code representing different functionality and create different methods appropriately.
He has also authored the book, Building Web Apps with Spring 5 and Angular.
Latest posts by Ajitesh Kumar (see all)
- Kerala to lead Blockchain Training in India - June 20, 2018
- Blockchain & Smart Contract for Quick Insurance Payouts? - June 19, 2018
- Lessons on Blockchain & Cryptocurrency for MBA Students - June 18, 2018