Kotlin Code Smell 011 - God Objects
An object that knows or does more than it should.
Play this article
TL;DR: Don't take too many responsibilities. use the single responsibility principle
Problems
Cohesion
Coupling
Solutions
Split responsibilities.
Follow the Single Responsibility Principle.
Follow The Boy Scout Rule.
Examples
- Libraries
Exceptions
Sample Code
Wrong
class Soldier {
fun run() {}
fun fight() {}
fun driveGeneral() {}
fun clean() {}
fun fire() {}
fun bePromoted() {}
fun serialize() {}
fun display() {}
fun persistOnDatabase() {}
fun toXML() {}
fun jsonDecode() {}
//...
}
Right
class Soldier {
fun run() {}
fun fight() {}
fun clean() {}
}
Conclusion
In Object-Oriented Programming, we will distribute responsibilities among many objects.