# Code Smell 202 - God Constant Class

> TL;DR: Don't define too many unrelated constants in the same class. Don't pile up the junk together.

# Problems

- Bad Cohesion

- High Coupling

- Magic Numbers

- Single Responsibility principle violation

# Solutions

1. Break the contents following Real World responsibilities using the [MAPPER](https://maximilianocontieri.com/what-is-wrong-with-software).

# Context

This is a special case of a God Object restricted only to constant definitions.

The repository can be a class, file, or JSON. 

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/402d6689b9a9bdef6a0cedf4e8974805)

```csharp

public static class GlobalConstants
{
    public const int MaxPlayers = 10;
    public const string DefaultLanguage = "en-US";
    public const double Pi = 3.14159; 
}


```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/d26aa5ca0e29c906689c7e0b3be8cd54)

```csharp

public static class MaxPlayersConstants
{
    public const int MaxPlayers = 10;    
}

public static class DefaultLanguageConstants
{
    public const string DefaultLanguage = "en-US";
}

public static class MathConstants
{
    public const double Pi = 3.14159;
}


```

# Detection

[X] Semi-Automatic

We can tell our linters to warn us of too many constants' definitions against a preset threshold. 

# Tags

- Coupling

# Conclusion

Finding correct responsibilities is one of our primary tasks when designing software.

# Relations

%[https://maximilianocontieri.com/code-smell-14-god-objects]

%[https://maximilianocontieri.com/code-smell-29-settingsconfigs]

%[https://maximilianocontieri.com/code-smell-02-constants-and-magic-numbers]

# More Info

[Medium](https://bytedev.medium.com/the-god-constant-class-30d82cd4f677)

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Aaron Burden](https://unsplash.com/@aaronburden) on [Unsplash](https://unsplash.com/images/things/book)
    
* * *

> If someone says their code was broken for a couple of days while they are refactoring, you can be pretty sure they were not refactoring.

_Martin Fowler_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

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