Yonatan Karp-Rudin
Posted on • Originally published atyonatankarp.com on
Kotlin Code Smell 11 - God Objects
TL;DR: Avoid taking on excessive responsibilities. Use the single responsibility principle.
Problems
Lack of cohesion
Tight coupling
Solutions
Split responsibilities.
Follow theSingle Responsibility Principle.
FollowThe Boy Scout Rule.
Examples
- Libraries
Exceptions
Sample Code
Wrong
classSoldier{funrun(){}funfight(){}fundriveGeneral(){}funclean(){}funfire(){}funbePromoted(){}funserialize(){}fundisplay(){}funpersistOnDatabase(){}funtoXML(){}funjsonDecode(){}//...}
Right
classSoldier{funrun(){}funfight(){}funclean(){}}
Conclusion
In Object-Oriented Programming, we distribute responsibilities among multiple objects.
Stay updated with my latest thoughts and ideas by registering for mynewsletter. Connect with me onLinkedIn orTwitter. Let's stay connected and keep the conversation going!
More info
Credits
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse