The term code smells was invented by Kent Beck in the book Refactoring : improving the design of existing code by Martin Fowler + Kent Beck, John Brant, William Opdyke, Don Roberts
He said in their book:
"A code smell is a surface indication that usually corresponds to a deeper problem in the system."
Common Code Smells:
1.Divergent Change
Symptom:
Divergent change occurs when one class is commonly changed in different ways for different reasons.
Reason for the problem:
Violation of the Single Responsibility Principle (SRP)
Treatment:
Split up the behavior of the class via Extract Class.
Combine the classes through inheritance (Extract Superclass and Extract Subclass).
2. Duplicate Code
Symptoms:
Two code fragments look almost identical.
Reason for the problem:
Multiple programmers are working on different parts of the same program at the same time
Rushing to meet deadlines
No frequent re-factoring
Treatment:
Extract Method
Use Template Method pattern
Extract Superclass
3. Long Method
Symptoms:
A method contains too many lines of code.
Reason for the problem:
Mindset (harder to create a new method than to add to an existing one)
Treatment:
Extract Method
4. Large Class
Symptoms:
A class contains many fields/methods/lines of code.
Reason for the problem:
Classes usually start small. But over the time, they get bloated as the program grows.
Place a new feature in an existing class than to create a new class for the feature.
Treatment:
Extract Class
Extract Subclass
Extract Interface
5. Long Parameter List
Symptoms:
More than three or four parameters for a method.
Reason for the problem:
Poor coding practice
Treatment:
Replace Parameter with Method Call.
Pass the object itself to the method
Introduce Parameter Object.
6. Dead Code
Symptoms:
A variable, parameter, field, method or class is no longer used (usually because it is obsolete).
Reason for the problem:
No time to clean up the old code
Treatment:
Delete unused code and unneeded files.
Use tools to identify dead code
7. Shotgun Surgery
Symptoms:
Making any modifications requires that you make many small changes to many different classes.
Reason for the problem:
A single responsibility has been split up among a large number of classes.
Treatment:
Proper abstraction
8. Primitive Obsession
Symptoms:
Using primitive data types (such as int, string, or constants) instead of defining a new class
For example, using an integer to represent an amount of money or a string for a phone number.
Reason for the problem:
Easy programming
Treatment:
Replace Data Value with Object
9. Feature Envy
Symptoms:
A method accesses the data of another object more than its own data.
Reason for the problem:
This smell may occur after fields are moved to a data class.
Treatment:
Data and functions that use this data are changed together