Thursday, December 26, 2024

Switching the Order – The Daily WTF

Programming LanguageSwitching the Order - The Daily WTF


Let’s say you had 6 different labels to display. There are many, many ways to solve this problem. Most of them are fine. Some of them are the dreaded loop-switch anti-pattern.

Julien unfortunately inherited one of those.

for($i=0;$i<=5;$i++) {

    switch($i) {

                case 1 :
            print('<p>Activé</p>');
            break;
        case 0 :
            print('<p>Désactivé</p>');
            break;
        case 2 :
            print('<p>En cours d\'inscription</p>');
            break;
        case 3 :
            print('<p>En cours de désinscription</p>');
            break;
        case 4 :
            print('<p>Désinscrit</p>');
            break;
        case 5 :
            default :
            print('<p>Marqué invalide</p>');
        break;
    }
}

Indentation in the original.

What’s beautiful in this, is that we can see that the order of the fields changed. Originally, it was meant to be “Activé”, then “Désactivé”. But for whatever reason, that wasn’t what needed to be on the page, so they flipped the order… by changing the values in the case statement.

It’s that bit there that really made this code special, to me. The out-of-order case is the annoying choice that makes bad code truly awful.

Check out our other content

Check out other tags:

Most Popular Articles