Skip to content

Latest commit

 

History

History
107 lines (65 loc) · 2.73 KB

File metadata and controls

107 lines (65 loc) · 2.73 KB

Code Smell 14 - God Objects

Code Smell 14 - God Objects

An object that knows too much or does too much.

TL;DR: Don't take too many responsibilities.

Problems

  • Cohesion

  • Coupling

Coupling - The one and only software design problem

Solutions

  • Split responsibilities.

  • Follow Single Responsibility Principle.

  • Follow The Boy Scout Rule.

Examples

  • Libraries

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.

Exceptions

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

Code Smell 34 - Too Many Attributes

Code Smell 202 - God Constant Class

Code Smell 124 - Divergent Change

More Info

Credits

Photo by Francisco Ghisletti on Unsplash


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code