• (nodebb)

    This is gold! A For-Case that doesn't need the For nor the Case. 🤣

  • Zatapatique (unregistered)

    It could be so much improved:

    if (frist) step1(); if (secnod) step2(); ....

  • 516052 (unregistered)

    I feel cheated. There is no switch, only if.

  • Lurk (unregistered)

    I think this may be the result of a dead requirement. At some point there was a perceived need to be able to execute steps 1 - n in a runtime determined order and the order of steps would be handed in as an array, list, whatever. The requirement got dropped or it was realised that there were no circumstances in which the steps would ever be run in any but one order, but the (part implemented?) code didn't get cleaned up properly.

  • (nodebb) in reply to Lurk

    Or perhaps there was some additional logic at the beginning/end of that loop which would run between each step, but it got removed at some point?

  • (nodebb)

    Wait, maybe I'm getting old and senile but isn't that just this?

    step1();
    step2();
    step3();
    finalStep();
    
  • (author) in reply to MaxiTB

    You're more with it than the developers responsible!

  • (nodebb)

    Somone started with a for-loop and then got told about this newfangled thing called a "race condition" ....

  • Goose (unregistered) in reply to MaxiTB

    Yes, yes it is.

  • (nodebb)

    Ah okay. I thought it can't be that easy and there's some hidden thing I missed.

    Welp, I guess there's always a complicated way to waste processing resources lol

  • (nodebb)

    This is gold! A For-Case that doesn't need the For nor the Case. 🤣

    You can even do a For-Case using recursion!

  • (nodebb)

    It's been in the code base for some time, so she's not entirely certain where it came from, or what the company's code review practices were like at the time.

    Once upon a time some coding jobs billed by LOC so now I'm wondering about the actual reason for the extra lines. 🤔 If it turns out this code was intentionally inflated it'd be pure genius because I don't think any LOC measurement algorithm of the era would be able to catch it and that's 175% extra for the exact same functionality.🤑

  • Scragar (unregistered) in reply to Lurk

    I have seen a similar pattern used before when steps need to be skipped in a way that makes it implausible to use regular ifs.

        for (i = 0; i < 10; i++) {
            if (i == 1) {
                 // some logic in step 1 that might update i to a different number if steps aren't needed
            }
            if (i == 2) {
                 // some logic in step 2 that might update i to a different number if steps aren't needed
            }
        }
    

    The place I most recently saw it was during an appointment booking system logic. It initialised it with a step from the order, then it'd attempt to do the next thing in the list, and that might do a bunch of skipping around in steps to find appointments based on other factors. So say you're booking a home visit for an elderly patient, we want to give preference to doctors they've worked with previously, but also get the appointment soon, and ideally while a doctor is already close, and maybe the patient has already expressed a desire to only be seen by doctors of a particular gender. There's a lot of little factors to consider and sometimes it's worth shuffling things around and trying a few variations to find the best fit for all the factors. Of course we didn't use numbers, we used constants labelled after the step names and sometimes it'd repeat steps with different parameters to try finding the best possible fit.

    It got fairly complicated, but it worked really well for the end users which was the important thing.

  • Loren Pechtel (unregistered)

    Two likely causes:

    1. The loop used to do something else, also. Especially true in the old days before threads, sometimes the only answer was to break up an expensive calculation.

    2. It used to be possible to skip around. Maybe a step fails and kicks it back to an earlier state to try something else.

    In either case, I think we are seeing the degenerate form of something that used to make sense.

  • (nodebb) in reply to Loren Pechtel

    Ah, it would be really useful in this case to know what language we are talking about. That's a pretty please feature I'm missing on this site since long before I started posting here decades ago. Without it, all we got is the Java-smell with the camel case name plus the for loop syntax and the naming of the iteration variable (naming smell leftover from C).

    For Java itself, I honestly can't remember a time when the Thread object was not around (so at least since 1.2) and for JavaScript, low level multi-threading made little sense because it was running in a browser sandbox and you usually used event driven/callback based implementations especially before async-wait was available.

    That said, if you want to break up a function you implement it by having a state machine in an iteration not the state in the iteration. So it would look something like this:

    var todo = false;
    var state = 1;
    
    while(!isCompleted)
    {
      switch(state)
      {
        case 1: state = step1(); break;
        case 2: state = step2(); break;
        case 3: state = step3(); break;
        case 4: state = stepfinal(); break;
        case 5: isCompleted = true; break;
      }
    }
    

    I think the easiest explanation is the best for this article, this code was just written by someone clueless or GenAI. Occam's razor :-)

  • David Harper (unregistered)

    A good optimising compiler would inline that mess. A really good optimising compiler would also deliver a 500 volt electric shock to the developer who wrote it.

  • 516052 (unregistered) in reply to Scragar

    That is literally a use case for GOTO and you know it.

  • 516052 (unregistered) in reply to David Harper

    Speaking of, it was always my lifelong dream (that sadly shall newer be) to write a universal pessimising compiler. As in, a compiler that pessimism every possible metric but without introducing bugs.

  • 516052 (unregistered)

    EDIT: Universal as in, actually works and is not a joke and handles serious languages fully and with 100% correctness and standard compliance. Not just as a joke.

  • (nodebb) in reply to MaxiTB

    No, it's:

    push stepfinal push step3 push step2 jmp step1

    Addendum 2025-12-24 06:04: Ugh, line breaks got deleted, imagine a break after each instruction.

  • 516052 (unregistered) in reply to zomgwtf

    You did mention conditional changes to the control flow though. As in, under certain situations Step 2 might alter something so that Step 3 might not execute. That sort of stuff. In which case, GOTO and a big file of code pages long is the perfect answer.

    Well that or a bunch of function calls. But a geezer can dream.

  • (nodebb)

    I have seen a similar pattern used before when steps need to be skipped in a way that makes it implausible to use regular ifs.

        for (i = 0; i < 10; i++) {
            if (i == 1) {
                 // some logic in step 1 that might update i to a different number if steps aren't needed
            }
            if (i == 2) {
                 // some logic in step 2 that might update i to a different number if steps aren't needed
            }
        }
    

    Indeed, you describe a state machine, which is a sensible idea. The For-Case pattern is also, I suppose, a kind of state machine as well. The simplest sort of state machine, which visits each state in sequence.

  • (nodebb) in reply to Loren Pechtel

    in the old days before threads

    So, like, the 1950s or the first half of the 1960s? By the end of the 1960s, IBM had invented something under a different name that was largely the same as what we call threads.

  • 516052 (unregistered) in reply to Steve_The_Cynic

    It took a while to trickle down to the rest of us though. Even today, not everyone is up to date, up to code, up to budget or up to management permission to go and use the fresh new thing that just came out.

  • (nodebb)

    Obviously someone thought "Hey, I need to do 4 things, this calls for a for loop!", implemented it this way, and no one thought anything of it.

    But I like the thoughts along the lines of "If step 3 fails go back to step 1" and such.

  • no name (unregistered) in reply to Loren Pechtel

    In either case, I think we are seeing the degenerate form of something that used to make sense.

    Or just accept the idea that some people are just stupid. Not every problem has a logical solution when it comes to human beings.

Leave a comment on “A Case of Old Code”

Log In or post as a guest

Replying to comment #688992:

« Return to Article