Write Clean Code and Get Rid of Code Smells with Real Life Examples

Code smells are a set of common signs which indicate that your code is not good enough and it needs refactoring to finally have a clean code. In this article, I am going to explain the code smells with real-life examples from real projects on GitHub and show you the best way to refactor these smells and clean things up. I also suggest you read this article afterward. “Advanced Coding Skills, Techniques, and Ideas.”

Duplicated Code and Logic Code Smell

It is common that developers are kind of lazy, which is very good in so many ways; however, being lazy and copy/past lines of code is not a proper behavior. It could lead to the most common code smell which is the logic duplication as in the following example.

So, to get rid of this code smell we need to extract the red part into a separate method which helps us to reuse it wherever we need.

Long Methods and Classes Code Smell

I believe we all made that mistake when we needed to add another if() or for() statements to an existing method in order to validate user inputs or to check if the user is logged in. So long story short, you shouldn’t do that. If you need such validation, then create its own method. The perfect method should be between 4 to 20 lines. If it is more than 20 line, you probably can extract a couple of lines into a new method. Same rules here for classes too, smaller is better especially if you apply the Single Responsibility Principle.

Duplicated Methods in the Same or Different Class Code Smell

Another code smell that needs to be taken into consideration is when you have two methods that do the same functionality. The following image should make it clear.

Class Divergent Change Code Smell

If you ever read about the SOLID principles before, especially the Single Responsibility, then you know that one class should have only one reason to change. This means a User class shouldn’t have a function related to products or file conversion. You can easily fix this code smell by extracting the unrelated method to a new class like Product class or FileSystem class.

Shotgun Surgery Code Smell

It is the exact opposite of divergent change. This code smell will make you change many classes for one single reason which means that many classes have the same reason to change in order to apply one behavior. For example, you need to create a new user rule such as ‘Supper-Admin’ then you found yourself must edit some methods in Profile, Products and Employees classes. In that case, consider grouping these methods into one single class so this new class will have a single reason to change.

Feature Envy Code Smell

Sometimes you found a method in your class that extensively makes use of another class. In that case, you may consider moving this method to the other class it uses. Have a look at the next image. Wouldn’t it be better if the getFullAddress() becomes a part of ContactInfo class instead of User class since all it does is to use ContactInfo methods.

Data Clumps Code Smell

Sometimes you find so many functions that almost take the same parameters list. This kind of parameters, that is always used together, causes the data clumps code smell. Take a look at the next example; you will find that almost all kinds of reservation require the passport information.

In that case, it would be much better to move the passport information into its own class then pass the PassportInfo object to the reservation methods. This is an excellent example of code reuse and remembers that long parameters list can lead to code failure, conflict and difficult unit testing.

About Mohamed Aladdin

Senior Developer, love to work on challenging enterprise applications following best practices and industry standards. exciting about new technology and applying it in real projects.

Leave a comment

Your email address will not be published. Required fields are marked *