An object that knows too much or does too much.
TL;DR: Don't take too many responsibilities.
Problems
Cohesion
Coupling
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Coupling: The one and only software design problem
Maxi Contieri ・ Feb 6 '21
Solutions
- Split responsibilities.
- Follow Single Responsibility Principle.
- Follow The Boy Scout Rule.
Examples
- Libraries
Exceptions
Sample Code
Wrong
class Soldier {
run(){}
fight(){}
driveGeneral(){}
clean() {}
fire() {}
bePromoted() {}
serialize() {}
display() {}
persistOnDatabase() {}
toXML() {}
jsonDecode(){}
//...
}
Right
class Soldier {
run() {}
fight() {}
clean() {}
}
Detection
Linters can count methods and warn against a threshold.
Tags
- Cohesive
Conclusion
Libraries were fine in the 60. In Object-Oriented Programming, we will distribute responsibilities among many objects.
Also Known as
- Large Class
Relations
More info
Credits
Photo by Francisco Ghisletti on Unsplash
This article is part of the CodeSmell Series.
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
Last update: 2021/06/21
Top comments (2)
For those who might pass by, this problem can be solved with the composition pattern.
indeed. but it might be overdesign sometimes