Yonatan Karp-Rudin
Yonatan Karp-Rudin

Follow

Yonatan Karp-Rudin

Follow
Kotlin Code Smell 011 - God Objects

Kotlin Code Smell 011 - God Objects

An object that knows or does more than it should.

Yonatan Karp-Rudin's photo
Yonatan Karp-Rudin
·Dec 16, 2022·

1 min read

Play this article

Table of contents

  • Problems
  • Solutions
  • Examples
  • Exceptions
  • Sample Code
  • Conclusion
  • More info
  • Credits

TL;DR: Don't take too many responsibilities. use the single responsibility principle

Problems

  • Cohesion

  • Coupling

Solutions

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.

More info

Credits

 
Share this