DEV Community

Cover image for Code Smell 202 - God Constant Class
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 202 - God Constant Class

Constants should be together to find them easily

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.

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


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


Enter fullscreen mode Exit fullscreen mode

Right


public static class MaxPlayersConstants
{
    public const int MaxPlayers =