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.
-
Bad Cohesion
-
High Coupling
-
Magic Numbers
-
Single Responsibility principle violation
- Break the contents following Real World responsibilities using the MAPPER.
This is a special case of a God Object restricted only to constant definitions.
The repository can be a class, file, or JSON.
public static class GlobalConstants
{
public const int MaxPlayers = 10;
public const string DefaultLanguage = "en-US";
public const double Pi = 3.14159;
}
public static class GameConstants
{
public const int MaxPlayers = 10;
}
public static class LanguageConstants
{
public const string DefaultLanguage = "en-US";
}
public static class MathConstants
{
public const double Pi = 3.14159;
}
[X] Semi-Automatic
We can tell our linters to warn us of too many constants' definitions against a preset threshold.
- Coupling
Finding correct responsibilities is one of our primary tasks when designing software.
Code Smell 29 - Settings / Configs
Code Smell 02 - Constants and Magic Numbers
Code Smells are my opinion.
Photo by Aaron Burden on Unsplash
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
Software Engineering Great Quotes
This article is part of the CodeSmell Series.