
An object that knows too much or does too much.
TL;DR: Don't take too many responsibilities.
Problems
Cohesion
Coupling
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.
Last update: 2021/06/21
About the author
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Comments
Join the discussionNo comments yet. Be the first to comment.
Code Smells
In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.





