# 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

%[https://maximilianocontieri.com/coupling-the-one-and-only-software-design-problem]

# Solutions

- Split responsibilities.
- Follow Single Responsibility Principle.
- Follow [The Boy Scout Rule](https://medium.com/@biratkirat/step-8-the-boy-scout-rule-robert-c-martin-uncle-bob-9ac839778385).

# Examples

- Libraries

# Exceptions

- [Facades](https://en.wikipedia.org/wiki/Facade_pattern)

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/9e31898b70c00fcb2d71d6d9a47be02d)
```javascript
class Soldier {
   run(){}
   fight(){}
   driveGeneral(){}
   clean() {} 
   fire() {} 
   bePromoted() {}
   serialize() {}
   display() {} 
   persistOnDatabase() {}
   toXML() {}
   jsonDecode(){}
  
  //...     
  }
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/93818a16a693b7371c8a81670ef522e5)
```javascript
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

%[https://maximilianocontieri.com/code-smell-34-too-many-attributes]

# More info

- [Wikipedia](https://en.wikipedia.org/wiki/God_object)
- [Refactoring Guru](https://refactoring.guru/es/smells/large-class)
- [Coupling](https://maximilianocontieri.com/coupling-the-one-and-only-software-design-problem)

# Credits

Photo by <a href="https://unsplash.com/@tank_ghisletti">Francisco Ghisletti</a> on <a href="https://unsplash.com/s/photos/greek-god-statue">Unsplash</a>

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]

*Last update: 2021/06/21*
