• some guy (unregistered)

    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.

  • some guy (unregistered)

    Addendum: in C++, you would use do-while(false), but that may have been unavailable here, too.

  • (nodebb)

    My suspicion about the purpose is that it's meant to easily disable blocks of code- change the first line to boolean loop = false and you effectively skip the block.

    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.

    boolean loop = true;
    while (loop) {
        // fake loop to break out of
        loop = false;
        doesStuff();
    
        if ( some_condition() )
            break;  // break out of the fake loop!
    
        moreStuff();
        etc();
    }
    

    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.

  • TheCPUWizard (unregistered)

    @Steve - "snecod" is always a WTF :)

  • (nodebb)

    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."

  • (nodebb) in reply to Steve_The_Cynic

    And thrid for being seconds too late for a frist joke

    Also you spelled "secnod" wrong.

  • Mike (unregistered)

    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.

  • Sauron (unregistered)

    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.

  • Vera (unregistered)

    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.

  • (nodebb) in reply to Steve_The_Cynic

    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();

  • Sauron (unregistered) in reply to Vera

    Sounds credible

  • Tim (unregistered) in reply to Melissa U

    No that doesn't work

  • NotAThingThatHappens (unregistered)

    Captcha is misbehaving, it auto triggers after a while.

  • (nodebb) in reply to Vera

    Someone complains, "This while(true) loop makes no sense! Having a constant value there makes no sense! Fix it!"

    The correct response to that is, "I'm not a neurosurgeon, so I can't fix you so it makes sense."

  • Hal (unregistered) in reply to Steve_The_Cynic

    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.

  • Naomi (unregistered) in reply to Melissa U

    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:

    doSomething();
    magic:
    {
        doSomethingMore();
        if (wizardryDeclaredIllegal()) break magic;
        doSomethingNotAlways();
    }
    doSomethingElse();
    
  • (nodebb)

    $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 "}"

  • (nodebb) in reply to Bananafish

    $20 says the variable loop is static and that someplace it will be set to 'false' and the code will never execute.

    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 - an if() would do it just as well.

  • SRI GANGA PACKERS & MOVERS INTERNATIONAL PVT. LTD. (unregistered)

    "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.

  • (nodebb)

    This is about replacing goto with break.

  • Fizzlecist (unregistered)

    My first thought was that somewhere deep in the bowels of one of the methods it calls is the line "loop = true;"

  • I must be doing it wrong then (unregistered)

    $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.

  • SomeGuyOnTheInternet (unregistered)

    My guess: parsing a file, and this pattern is used to avoid a GOTO statement, by breaking out of the "loop".

  • Peter of the Norse (unregistered) in reply to Steve_The_Cynic

    I’ve had to deal with something even worse but solving the same problem

    try {
        valid = doSomeThing();
        if (!valid) {
            throw new Exception();
        }
        valid = doSomeThingElse();
            if (!valid) {
            throw new Exception();
        }
    } catch (Exception e) {
        doIfNotValid();
    }
    

    We only found this problem when a dev updated a method to throw IllegalArgumentException when it got bad data instead of just corrupting the database

  • Your Name (unregistered)

    Obligatory The Real Real WTF is web development. Over and over again. How could we let it get that bad that much.

  • (nodebb)

    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…

  • Worf (unregistered) in reply to some guy

    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.

  • (nodebb)

    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.

  • RLB (unregistered)

    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.

  • Álvaro González (github)

    JSP is what people have in mind when they bash PHP.

  • Duke of New York (unregistered) in reply to MaxiTB

    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.

  • piqpoq (unregistered) in reply to Domin Abbus

    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."

Leave a comment on “Broken Loop”

Log In or post as a guest

Replying to comment #:

« Return to Article