• (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.

Leave a comment on “A Case of Old Code”

Log In or post as a guest

Replying to comment #688918:

« Return to Article