Description
Bloater is a piece of code, method, or class that has gradually become too huge to manage, and needs immediate attention.
These evolved over time as the code becomes bigger and more complex, and nobody makes an effort to refactor.
Here is a list of such code smells.
- Long Method
- Long Class
- Long Parameter List
- Primitive Obsession
- Data Clumps
Long Method
Problem: A method contains too many lines of code. Generally, any method longer than ten lines should make you start asking questions.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Extract Method
- Replace Temp with Query
- Introduce Parameter Object
- Preserve Whole Object
- Replace Method with Method Object
- Decompose Conditional
Long Class
Problem: A class contains many fields/methods/lines of code.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Extract Class
- Extract Subclass
- Extract Interface
- Duplicate Observed Data
Long Parameter List
Problem: More than three or four parameters for a method.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Replace Data Value with Object
- Introduce Parameter Object
- Preserve Whole Object
- Replace Type Code with Class
- Replace Type Code with Subclass
- Replace Type Code with State/Strategy
- Replace Array with Object
Primitive Obsession
Problem: This can include any of the below.
- Use of primitives instead of small objects for simple tasks (such as currency, ranges, special strings for phone numbers, etc.)
- Use of constants for coding information (such as a constant
USER_ADMIN_ROLE = 1
for referring to users with administrator rights.) - Use of string constants as field names for use in data arrays.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Replace Parameter with Method call
- Preserve Whole Object
- Introduce Parameter Object
Data Clumps
Problem: Sometimes different parts of the code contain identical groups of variables (such as parameters for connecting to a database). These clumps should be turned into their own classes.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Extract Class
- Introduce Parameter Object
- Preserve Whole Object
Overall
We now know how to find and resolve bloaters in the code to improve the code quality.