Coding Principles

|

5

minutes read

Code Smells

Travis

Ponyo

September 19, 2022

feature image

Where does that code smell come from? Line 2134.

Code smells are small mistakes in the programming process, which accumulate gradually and become big problems that hinder the product development process.

Having code smells does not mean that the program will not work, it would still give an output, but it may slow down processing, and increase the risk of failure and errors while making the program vulnerable to bugs in the future.

👃 Smelly code contributes to poor code quality and hence increases the technical debt.

How to spot code smells

Code smells usually indicate bigger problems. But as the name suggests, they are sniffable or quick to spot. Easy, if your nose twitches while looking at the codes, and you are so close to cursing out loud, it is probably a code smell.  (just kidding)

Here are some common signs of smelly codes:

  1. Rigidity: The software is difficult to change. A small change causes a cascade of subsequent changes.
  2. Fragility: Just one single change, and the whole software collapses.
  3. Immobility: Parts of the codes are un-reusable in other projects due to the high risks and large efforts involved.
  4. Unnecessary Complexity: The codes are too complex to maintain.
  5. Duplicated codes: This is the greatest sin in programming, don’t repeat yourself.
  6. Opacity. The code is hard to understand.
a sheldon cooper spraying meme saying something smells your code

How to develop your “code nose”

Developing your "code nose" against code smells is something that should happen early in your programming career (if it is going to happen at all.)

Code smells can be easily detected with the help of automatic tools, for example, sonarqube.com, deepsource.io, etc. However, code review still remains necessary (and recommended!) since automated tools cannot detect all code smells, for example, design-related issues.

This is why code review is crucial in assisting developers to detect and deal with code smells early on. To perform a thorough code review, developers must be aware of the indicators of a code smell so that they can identify and correct any issues. Once you have detected the code smells, the next step is to refactor your codes.

But keep this in mind, the important thing is not the refactoring – it is learning to recognize the scent of your own code.

Code smells in real life

a bad luck brian meme spending hours removing a code smell adds another bunch in the process

Let’s take a look at a code smell and its solution:

This is Temporary Fields Code Smell, this happens when you have a class instance variables only used sometimes and go unused most of the time. This kind of code is hard to understand.

The solution is to transform them into parameters:

👃 If you want more examples of the stinkiest code imaginable, How to Write Unmaintainable Code is a good place to start. Stay tuned for more coding techniques from FABA Technology. Until next time!