The Story of "Code Smells"

The Story of "Code Smells"

ยท

3 min read

When talking about code smell, the first thought that come to my mind is the first application I wrote. When I wrote this application the one and only requirements is "Working" doesn't matter if you cannot maintain it. The approach I use to develop this application is Family Approach which is bring it all together ๐Ÿ˜œ, I literally put all functional code into only one class. Next day, I asked to change something inside the application and I stare my screen for 30 minutes just to decide where to start, maybe at that time I don't know what code smells is but I can smell there is something wrong about this code.

Code Smells

A code smell is a surface indication that usually corresponds to a deeper problem in the system.

  • Martin Fowler

Code smells are signs in the code that show there might be a bigger problem with the application or codebase. They're not mistakes, but they're like red flags that point to issues with how the code is structured or written. These issues can pile up over time, making it harder to maintain the code in the future. Even though the code might work fine for now, these smells could slow down development and cause problems later on, like performance or security issues. Same with story I told before, application that I made doesn't have any problem at all and working properly, but sure, it will bring the problem in the future (in my case, it on the next day). Here are example of some common code smells:

  • Duplicated Code: When the same or very similar code appears in multiple places within a codebase.

  • Long Method: A method or function that is excessively long, often spanning many lines of code. Long methods can be difficult to understand and maintain.

  • Large Class: Similar to a long method, a large class is one that contains a significant amount of code and functionality. Large classes can become unwieldy and difficult to manage.

  • Primitive Obsession: This occurs when primitive data types (such as integers or strings) are used to represent domain concepts that would be better served by custom objects or classes.

  • Inappropriate Intimacy: This refers to classes that are overly dependent on each other, often resulting in tight coupling.

  • Shotgun Surgery: This occurs when a single change to the codebase requires making multiple changes in different parts of the code.

There are also a lot more example of code smells, this sources from Refactoring Guru give a good and brief example about code smells, I recommend you to check it, happy reading!

That's all, Thanks for reading this far โœŒ๏ธ. I'm eager to hear your thoughts and stories about code smells.

ย