- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Chicken Feed
- Twofers
- Two-faced
- Boxing Day Math
- Michael's Holiday Snaps
- Anonymice
- A Horse With No Name
- On the Dark Side
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
This is gold! A
For-Casethat doesn't need theFornor theCase. 🤣Admin
It could be so much improved:
if (frist) step1(); if (secnod) step2(); ....
Admin
I feel cheated. There is no switch, only if.
Admin
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.
Admin
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?
Admin
Wait, maybe I'm getting old and senile but isn't that just this?
Admin
You're more with it than the developers responsible!
Admin
Somone started with a for-loop and then got told about this newfangled thing called a "race condition" ....
Admin
Yes, yes it is.
Admin
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
Admin
You can even do a For-Case using recursion!
Admin
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.🤑
Admin
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.
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.
Admin
Two likely causes:
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.
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.
Admin
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:
I think the easiest explanation is the best for this article, this code was just written by someone clueless or GenAI. Occam's razor :-)
Admin
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.
Admin
That is literally a use case for GOTO and you know it.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
Or just accept the idea that some people are just stupid. Not every problem has a logical solution when it comes to human beings.