Code Smell 193 - Switch Instead of Formula
Tags: webdev, javascript, beginners, programming
Published: 2023-01-27 17:53:14

*Which is better, declarative or shorter code?*

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

# Problems

- Readability

- [Premature Optimization](https://dev.to/mcsee/code-smell-20-premature-optimization-4moe)

# 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.

{% twitter 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://dev.to/mcsee/chatgpt-the-surprising-teacher-of-a-25-year-senior-programmer-53o0) and was not able to simplify it.

# Relations

{% post https://dev.to/mcsee/code-smell-36-switch-case-elseif-else-if-statements-h6c %}

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

# More Info

{% twitter 1615204074588180481 %}

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

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

# Disclaimer

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

* * *

> 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_

{% 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 %}