Code Smells: What’s so smelly about the code?
The word code smell was proposed by Kent Beck when contributing on the book Refactoring by Martin Fowler. A code smell is a surface indication that usually corresponds to a deeper problem in the system. Smells are things that you feel are wrong, even if they perform the right operations. Smells call for refactoring, to make the code look and feel better, to make it maintainable and extendable.
The different kinds of code smells can be grouped as follows:
- Bloaters
- Object Orientation Abusers
- Change Preventers
- Dispensables.
- Couplers.
Let’s discuss about each one of them briefly.
Bloaters
Bloaters are code, methods, and classes that have increased to such proportions making them hard to work upon. This kind of smell doesn’t get spotted right away but accumulates over the time as the program evolves. Potential candidates include:
- Long Methods
- Large Classes
- Primitive Obsession
- Long Parameter Lists
- Similar Code Clumps
Object-Orientation Abusers
All these smells are an incomplete or incorrect application of object-oriented programming principles. These smells typically include the following:
- Switch/If-else ladders.
- Temporary Feilds
- Alternate Classes with Different Interfaces.
Change Preventers
These smells mean that if you need to change something in one place in your code, you have to make many changes in other places too. Program development becomes much more complicated and expensive as a result. The following examples fall into this category:
- Divergent Changes
- Shotgun Surgery
- Parallel Inheritance Hierarchy
Dispensables
These are the components which are pointless, and whose absence would make the code much cleaner and easier to understand. These include the following:
- Comments
- Duplicate codes
- Lazy class declarations
- Dead Code
- God Classes/ Data Classes
Couplers
These smells lead to excessive coupling between classes. This category includes the following candidates:
- Method Chaining
- Middle Man to delegate responsibilities, even if the class performs one action.
- Violations of Law of Demeter
Code smells are something which is really hard to pick in the beginning but gradually becomes easy to spot with an increase in a programmer’s experience. Code Smells indicate nothing, but a flawed design and an intense need for refactoring. So do watch out for smelly code, and keep improving and keep refactoring. For further read do visit the link.
