Other Parts of This Series:

Refactoring Principles
Main Principle of Refactoring
The main motivation of refactoring is the Boy Scout rule, which is “Keep the camp ground cleaner than you found it.” In our words, “Keep the codebase cleaner and more readable than you found it.”
Definition
Refactoring is the process of restructuring existing code to improve its quality (like readability or performance) without altering its external or existing behavior.
What is Refactoring?
- It’s a cleanup process for code.
- It simplifies complex code, removes redundancies, and organizes it better.
- It just restructures the existing code without adding functionality.
- Example: Renaming variables to meaningful names or breaking a large function into smaller ones.
What is not Refactoring
- Adding new features.
- Fixing bugs.
- Changing how the program works for the user.
- Writing entirely new code.
Identify the Code Smells with Example
- Long Method: A method that’s too long and hard to understand.
Problem: Too much logic in one method.
- Large Class: A class with too many responsibilities.
Problem: Handles too many responsibilities.
- Primitive Obsession: Using basic data types instead of objects to represent concepts.
|
|
Problem: Using a dictionary instead of a User class.
- Long Parameter List: A method with too many parameters, making it hard to use.
Problem: Too many parameters.
- Data Clumps: Data fields that always appear together but aren’t grouped into a class.
Problem: Related data not grouped into a class or object.
- Switch Statements: Overuse of switch or if-else blocks instead of polymorphism.
Problem: Should use polymorphism instead.
- Temporary Field: Fields that are only used in some cases, making the class unclear.
Problem: Field not always relevant.
- Refused Bequest: A subclass that doesn’t use or need methods from its parent class.
Problem: Subclass doesn’t need inherited methods.
- Alternative Classes with Different Interfaces: Similar classes with inconsistent method names.
Problem: Similar functionality but inconsistent method names.
- Divergent Change: One class changes for many unrelated reasons.
Problem: One class handles unrelated responsibilities.
- Shotgun Surgery: A small change requires editing many parts of the code.
Problem: Changes require editing multiple parts of the code.
- Parallel Inheritance Hierarchies: Adding a class in one hierarchy forces changes in another.
Problem: Adding one class forces a new related class.
- Comments: Excessive comments instead of clear code.
Problem: Comment explains what clear code should do.
- Duplicate Code: Identical code in multiple places.
Problem: Repeated logic.
- Lazy Class: A class that doesn’t do enough to justify its existence.
Problem: Class doesn’t justify its existence.
- Data Class: A class that only has fields and getter/setter methods.
Problem: Only holds data, no behavior.
- Dead Code: Unused code that serves no purpose.
Problem: Code that is never used.
- Speculative Generality: Over-engineered code for scenarios that might never happen.
Problem: Adding complexity for “future” use.
- Feature Envy: A method in one class depends too much on another class.
Problem: One class uses another’s data too much.
- Inappropriate Intimacy: Two classes interact too closely, violating encapsulation.
Problem: Accessing private fields of another class.
- Message Chains: A long chain of method calls to access an object.
|
|
Problem: Chaining too many calls.
- Middle Man: A class that adds no value and simply delegates tasks to another.
Problem: Adds no value; just passes calls.
- Incomplete Library Class: A library class is missing useful methods, leading to workarounds.
Problem: Missing useful functionality, requiring workarounds.
- Hidden Temporal Coupling: Code that requires methods to be called in a specific order but doesn’t make it clear.
Problem: Order of calls matters but isn’t clear.
References
- Book - Refactoring by Martin Fowler
- Blog - Refactoring.guru