Description
The excessive coupling between classes can cause code smells, which can be resolved through excessive delegation.
Here is a list of commonly observed code samples that cause excessive coupling.
- Feature Envy
- Incomplete Library Class
- Middle Man
- Inappropriate Intimacy
- Message Chains
Feature Envy
Problem: A method accesses the data of another object more than its own data.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Extract Method
- Extract Method
Middle Man
Problem: If a class performs only one action, delegating work to another class, why does it exist at all?
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Remove Middle Man
Inappropriate Intimacy
Problem: One class uses the internal fields and methods of another class.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Move Method
- Move Field
- Extract Class
- Hide Delegate
- Change Bidirectional Association to Unidirectional
- Replace Delegation with Inheritance
Message Chains
Problem: In code, you see a series of calls resembling $a->b()->c()->d()
.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Hide Delegate
- Extract Method
- Move Method
Incomplete Library Class
Problem: Sooner or later, libraries stop meeting user needs. The only solution to the problem—changing the library—is often impossible since the library is read-only.
Solution: Use the mentioned refactoring techniques to avoid these kinds of code smells.
- Introduce Foreign Method
- Introduce Local Extension
Overall
We now know how to find and resolve excessive coupling in the code to improve the code quality.