• Snecond! (unregistered)

    Code like this keeps one awake.

  • Sauron (unregistered)

    This code is absolutely stupid, they could have just replaced it with something like:

    print('<p>Désactivé</p>');
    print('<p>Activé</p>');
    print('<p>En cours d\'inscription</p>');
    print('<p>En cours de désinscription</p>');
    print('<p>Marqué invalide</p>');
    

    I'm not sure which language it is, but maybe they could even have defined all the HTML in a single multiline string and made just one call to print() .

    Also, if they're rendering HTML, they could arguably use a templating language in the first place, rather than dynamically generating the same 6 constant chunks of HTML in the same loop forever.

  • (nodebb)

    I'm still wondering mostly from which line of thought the loop-switch anti-pattern even arises.

    I've used similar things myself in some instances in Fortran though. But mostly, because there is code that remains the same in each loop iteration, with e.g. slightly different setup. Maybe a function call would be better in those places, but generally I dislike the idea of refactoring out part of the code into a separate function, if that call will ever only make sense in one specific context.

    In Python, the same situation could be solved typically with a for-each loop, e.g. (real code usually without magic constants 32, 33)

    for name, values in [
            ("acceleration", data[32, :]),
            ("force", data[33, :]),
    ]:
        write_the_output()
    

    In Fortran, this isn't possible directly, so I sometimes end up at

    do icase = 1, 2
        select case(icase)
        case(1)
            cname = "acceleration"
            values => data(32, :)
        case(2)
            cname = "force"
            values => data(33, :)
        case default
            error stop "invalid icase" ! <= important to catch typo bugs, often omitted
        end select
        ! output code here
    end do
    

    So I can see, how a "loop-switch" may start out, but the examples given here usually don't have the "shared code between iterations" part of it.

  • kythyria (unregistered) in reply to Sauron

    It looks like PHP, which already works as a templating language (textual, like most of them). That or Perl.

  • TF (unregistered)

    This is the kind of code WTF where you think it's obvious what is wrong and how to fix it but keep second-guessing yourself because it's so stupid.

  • (nodebb) in reply to Sauron

    I'm not sure which language it is

    French.

  • Álvaro González (github)

    This is PHP. They could have just typed the raw literal HTML code in the file. I would have thought it's physically impossible to write PHP without knowing that, but I guess I'm wrong.

  • (nodebb) in reply to Álvaro González

    I read "This is PHP" in James Earl Jone's voicing for "This... is CNN."

  • (nodebb)

    I've seen loop-switch used many times in code. I never figured out why it's better - why is it better to loop over something 10 times, doing 10 different things, rather than a single function calling 10 other functions in sequence? Or just making a massive function a few lines shorter by getting rid of the loop and the switch?

  • (nodebb)

    I expect the usual use case is some overlarge block of code is written using all variables that are set once (perhaps from input) at the top of the module or method and are effectively global to that method.

    Then somebody gets the idea to do exactly the same thing twice (or some other small and known number of reps) but with different inputs each time.

    The smart way is to refactor the big block of code into a method that takes the preset variables as parameters, or even build a holder class/struct to carry all the many method-globals across into the extracted method as a unit.

    Before the advent of good automated refactoring tools that was both hard and error-prone. Throwing a quick loop-and-case on the front to separately initialize all the method-global variables then leave the existing code below to run untouched is the cheap fast way to close the ticket.

    Of course the more spaghettified the whole blob of code is, and the more non-local side effects it has, the harder it is to refactor-extract that into a clean side effect-free method.

  • Maurizio (unregistered)

    The out of order thing actually show why this code is brilliant: to change the order, you do not need to move blocks of code around, you just need to change the indexes in the 'case:'. Very good for maintenance ... or may be not :)

Leave a comment on “Switching the Order”

Log In or post as a guest

Replying to comment #:

« Return to Article