- Feature Articles
- CodeSOD
- Error'd
- 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
I would guess that some instance, somewhere, once had a break statement to goto to the end of the loop. goto may not have been available or forbidden.
Admin
Addendum: in C++, you would use do-while(false), but that may have been unavailable here, too.
Admin
No, the clue is in the comment. It's just not a good example of it.
Let's modify the code a bit, and the comment will make more sense.
It's the deranged cousin of our friend "do_while_zero_with_breaks_to_avoid_gotos" that we've seen before on this site.
And therefore, it's a double WTF: frist for having a fake loop, and snecod for being the deranged cousin of the fake loop.
Addendum 2024-06-13 06:41: And thrid for being seconds too late for a frist joke.
Admin
@Steve - "snecod" is always a WTF :)
Admin
I feel your pain. In my predecessor's VBA code the is "while true" antipattern, littered with Exit Sub's. (and On Error Goto, of course.) There is a deep-seated suspicion that he once, somewhere, read an article claiming that "Goto is evil."
Admin
Also you spelled "secnod" wrong.
Admin
My thought is that it's a way to create a code block to visually distinguish that section of code. I still hate it, but it's the most reasonable explanation I can think of.
Admin
The comment is also wrong, it's not a fake loop, it's a real one, it's just a useless one because it only ever has exactly one iteration.
Admin
My guess: cargo-cult programming and poorly considered coding standards.
Someone complains, "This while(true) loop makes no sense! Having a constant value there makes no sense! Fix it!" and now it's a rule that all loop conditions have to be variables.
Someone else has a block of code that may need to be interrupted partway through, it's a Friday, the coffee just ran out, they're thinking of quitting... Whatever the reason, they make a "fake loop" so somewhere else in the loop body, they can just use a break to get out of it.
Yet again someone else runs into the same problem, sees how the other dev fixed it, and just blindly copy-pastes it. Repeat a few times.
The snippet is now standard practice in the code base, the PHBs get upset if it's not "done right, like we've been doing all along," and so the snippet gets copied around the codebase without second thought.
Admin
Exactly! They misused a while loop to implement GOTO.
Another WTF is that in Java, you don't need a while or if statement to do such thing. You can simply add code blocks:
doSomething(); { doSomethingMore(); if (shouldRunGoto) break; doSomethingNotAlways(); } hereIsTheGoto();
Addendum 2024-06-13 08:29: Hmh, why can't i edit my message?
doSomething();
{
doSomethingMore();
if (shouldRunGoto) break;
doSomethingNotAlways();
}
hereIsTheGoto();
Admin
Sounds credible
Admin
No that doesn't work
Admin
Captcha is misbehaving, it auto triggers after a while.
Admin
The correct response to that is, "I'm not a neurosurgeon, so I can't fix you so it makes sense."
Admin
This about the only plausible thing I can come up with as well. What I don't understand is what is the motivation; why not just a new function/method/sub (whatever this actually is) with an early return if you want this behavior, the only thing this does it let 'cheat' on variable scope maybe.
Admin
I think you need the "labeled block" syntax. It's pretty obscure and I actually had to look it up to make sure I had it right:
Admin
$20 says the variable loop is static and that someplace it will be set to 'false' and the code will never execute. Classic case of:
if (someone_else_is_doing_something) { do_nothing(); {
Addendum 2024-06-13 11:45: Sorry, last character should be "}"
Admin
I'll take your $20, then. If it was that, the "loop" could only ever execute once, because the loop itself sets the variable to
false
. But even then, there's still no reason to do it with a loop - anif()
would do it just as well.Admin
"Thank you for this enlightening post! Your breakdown of the challenges and opportunities in the modern job market is both comprehensive and thought-provoking. One aspect that particularly resonated with me is the emphasis on continuous learning and skill development. In today's rapidly evolving landscape, adaptability is indeed a key factor in staying relevant and competitive.I appreciate your practical suggestions for upskilling, such as online courses and networking events.
Admin
This is about replacing goto with break.
Admin
My first thought was that somewhere deep in the bowels of one of the methods it calls is the line "loop = true;"
Admin
$processed = true; // ...process the wobulator do { // do stuff to process the wobulator... ... $processed = true; } while ($processed = false); // ...process the fibrillator do { // do stuff to process the fibrillator... ... $processed = true; } while ($processed = false); etc, etc...
Why?
Because I can then get my IDE to "fold away whole sections of code" so that I can see the code / path I'm working on without getting "distracted".
Also, because the code is "sectioned", I can (if required) put in an additional "condition" to not execute that section of code.
Admin
My guess: parsing a file, and this pattern is used to avoid a GOTO statement, by breaking out of the "loop".
Admin
I’ve had to deal with something even worse but solving the same problem
We only found this problem when a dev updated a method to throw
IllegalArgumentException
when it got bad data instead of just corrupting the databaseAdmin
Obligatory The Real Real WTF is web development. Over and over again. How could we let it get that bad that much.
Admin
Putting on my naively optimistic hat… when debugging, with certain tools, there is a feature that allows “exit block”. I think some also have a “skip statement” feature, but perhaps not all.
Having this pattern allows someone to skip execution of a block of code when using the debugger, just by hitting “exit block” - without having to modify and recompile the code. Similarly, if they put a break point at the start of the
while
, they could update the loop variable using the debugger to skip the execution of the block in advance, even if their debugging tool has neither the “exit block” feature nor the “skip statement” feature.Still a WTF… but one that could be useful in some situations…
Admin
do .. while (0) is perfectly legitimate C code as well. It was used as a compiler independent way of doing bracing - older compilers didn't like having a { in the middle of code that wasn't prefaced with a conditional or loop construct. So if you needed to do a macro or something that performed a bit of work you often did "do { something(); something_else(); etc } while (0);" which would be compiler independent.
These days, compilers just handle { in the middle of a block of code just fine.
Admin
Oh, I know this one. It's the Java goto pattern. Because they don't have a goto, people come up with crazy constructs like that.
Admin
Snapping a bike chain! I (being a Dutchman) cycle to work every day and have run my chain off the gears more than once, but I've never managed to snap it. You have my sympathy and my respect, Remy.
Admin
JSP is what people have in mind when they bash PHP.
Admin
Java does have a (forward) goto, namely breaking from an arbitrary block, but no one teaches it. One might argue that no one should, but that doesn't seem to stop anyone from writing code like this.
Admin
It's an interesting possibility. Certainly an attitude a lot of people adopted without having read the original article or having read the original article closely. I periodically feel compelled to reiterate Dijkstra's end-of-paragraph statement that moderates the commonly adopted view: "I do not claim that the clauses mentioned are exhaustive in the sense that they will satisfy all needs, but whatever clauses are suggested (e.g., abortion clauses) they should satisfy the requirement that a programmer independent coordinate system can be maintained to describe the process in a helpful and manageable way."