# Code Smell 193 - Switch Instead of Formula

> TL;DR: Be declarative enough but no more.

# Problems

- Readability

- [Premature Optimization](https://maximilianocontieri.com/code-smell-20-premature-optimization)

# Solutions

1. Use a short version (or not).

2. Always favor readability >> Premature optimization.

3. Humans learn by examples, not by formulas.

4. Shorter is not always better.

# Context

Last week, a tweet went viral because of a missing formula.

%[https://twitter.com/JeroenFrijters/status/1615204074588180481]

It is the DigiD digital authentication iOS app in the Netherlands.

# Sample Code

## Wrong?

[Gist Url]: # (https://gist.github.com/mcsee/0a2979db94ff5288a342e2846155d955)
```csharp
private static string GetPercentageRounds(double percentage)
        {
            if (percentage == 0)
                return "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪";
            if (percentage > 0.0 && percentage <= 0.1)
                return "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪";
            if (percentage > 0.1 && percentage <= 0.2)
                return "🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪";
            if (percentage > 0.2 && percentage <= 0.3)
                return "🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪";
            if (percentage > 0.3 && percentage <= 0.4)
                return "🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪";
            if (percentage > 0.4 && percentage <= 0.5)
                return "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪";
            if (percentage > 0.5 && percentage <= 0.6)
                return "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪";
            if (percentage > 0.6 && percentage <= 0.7)
                return "🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪";
            if (percentage > 0.7 && percentage <= 0.8)
                return "🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪";
            if (percentage > 0.8 && percentage <= 0.9)
                return "🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪";

            return "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵";
        }
    }
}

// Full source
// https://github.com/MinBZK/woo-besluit-broncode-digid-app/blob/master/Source/DigiD.iOS/Services/NFCService.cs
```

## Right?

[Gist Url]: # (https://gist.github.com/mcsee/e24ffc9ad7e587f44862ed7dff22e1b7)
```csharp
private static string GetPercentageRounds(double percentage)
{
    string dots = "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪";
    int blueDots = (int) Math.Truncate (percentage* 10);
    int startingPoint = 10-blueDots;
    return dots. Substring(startingPoint, 10);
}

```

# Detection

[X] Semi-Automatic

This is a semantic smell. In this case, we can count the number of if clauses.

# Tags

- Readability

# Conclusion

You can read the original [Twitter thread](https://twitter.com/JeroenFrijters/status/1615204074588180481) to take your own conclusions. There's some serious debate and, of course, several premature optimizators bringing obscure and unneeded solutions with *(O) log(n)* complexity and stupid benchmarks evidence for a loop that executes only once.

And lots of memes.

As a final conclusion, I asked [ChatGPT](https://maximilianocontieri.com/chatgpt-the-surprising-teacher-of-a-25-year-senior-programmer) and was not able to simplify it.

# Relations

%[https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements]

%[https://maximilianocontieri.com/code-smell-20-premature-optimization]

# More Info

%[https://twitter.com/JeroenFrijters/status/1615204074588180481]

- [GitHub Repo](https://github.com/MinBZK/woo-besluit-broncode-digid-app/)

%[https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever]

# Disclaimer

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

* * *

> There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.

_C. A. R. Hoare_

%[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]
