Has anyone ever written code that ended up tangled like the messy ball of yarn on the left? Have you ever wondered why it gets so tangled, even when we often think we've designed it perfectly? So, what do we need to do to make our code look as neat and beautiful as the spool of thread on the right? In the upcoming articles, I'll be gradually writing more about this for you all to read, haha! ^^y
In this article, let's first get to know the root causes that turn our code into a tangled mess (Spaghetti Code). We collectively call these causes Code Smells—basically, something that seems a bit "off" and gives us a hint that our code is definitely going to cause problems down the line.
Once we catch a whiff of these peculiar smells, the solution is to do Code Refactoring. This makes our code highly manageable, beautiful, and much easier to read. Anyone who drops by and sees your code will be full of praise, haha!
Next, let's familiarize ourselves with the different categories of Code Smells:
-
Bloaters: Code, functions, or classes that just keep expanding as we write, eventually growing into giant monsters that we absolutely do not want to mess with.
-
Object-Orientation Abusers: Incomplete implementations or the incorrect application of Object-Oriented Programming (OOP) principles.
-
Change Preventers: The classic situation where modifying code in one place impacts several other places, forcing you to chase down and fix a chain reaction of errors.
-
Dispensables: Junk code or anything useless that doesn't add any real value to the program—whether that’s making the code look better, perform better, or simply making it easier to understand.
-
Couplers: Code that is overly dependent and tied together so tightly that it cannot be separated. This can apply to specific code blocks, functions, or entire classes.