Code Smell 249 - Constants as Numbers
Tags: webdev, beginners, programming, tutorial
Published: 2024-04-25 02:26:38

*You map concepts to optimized numbers*

> TL;DR: Use real-world constants as constants

# Problems

- Bijection Violation

- Debugging 

- Premature Optimization

- Maintainability

- Type Safety

- Limited Extensibility

# Solutions

1. Use descriptive string constants

2. Use Enums

3. Create A [Polymorphic](https://en.wikipedia.org/wiki/Polymorphism_(computer_science)) Hierarchy

# Context

Numeric constants representing real-world concepts lack readability and make your code harder to understand. 

It's not clear what each number represents.

if you need to add more constants in the future or change the order, you'll have to update all occurrences of these numeric constants manually.

New values can introduce errors and make maintenance more difficult.

Saving attributes as *integers* to improve the persistence performance or space is a clear signal of [Premature Optimization](https://dev.to/mcsee/code-smell-20-premature-optimization-4moe).

# Sample Code

## Wrong

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

```csharp
public const FORMAT_JPG = 1;
public const FORMAT_GIF = 2;
public const FORMAT_PNG = 3;
```

## Right

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

```csharp
public const string FORMAT_JPG = "JPG";
public const string FORMAT_GIF = "GIF";
public const string FORMAT_PNG = "PNG";

// OR

public enum ImageFormat
{
    JPG,
    GIF,
    PNG
}
```

# Detection

[X] Semi-Automatic 

You can tell your linters to warn you about this usage, but they can show you some false positives.

# Tags

- Readability

# Level

[X] Beginner

# AI Generation

No models create this premature optimization problem when prompted to list constants.

# AI Detection

ChatGPT and Gemini detected this as a mistake.

# Conclusion

After this correction, your code will be cleaner readable, and self-explanatory for debugging.

You can read it and It's clear what each constant represents.

If you need to add more formats or change the order, you should update the enumeration definition without affecting the rest of the code. 

IDEs with auto-completion and error checking will give you better support to work with enums.

# Relations

{% post https://dev.to/mcsee/code-smell-02-constants-and-magic-numbers-obb %}

{% post https://dev.to/mcsee/code-smell-110-switches-with-defaults-1ibb %}

{% post https://dev.to/mcsee/code-smell-20-premature-optimization-4moe %}

# More Info

{% post https://dev.to/mcsee/how-to-get-rid-of-annoying-ifs-forever-1jfg %}

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Markus Krisetya](https://unsplash.com/@krisetya) on [Unsplash](https://unsplash.com/photos/person-in-white-shirt-and-blue-denim-shorts-standing-on-black-and-white-floor-Vkp9wg-VAsQ)
    
* * *

> Forget this world and all its troubles and if possible its multitudinous Charlatans - everything in short but the Enchantress of Numbers.

_Ada Lovelace_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}