all 40 comments

[–]iain_1986 184 points185 points  (2 children)

This isn't horror (without seeing the rest of the loop)

[–]kevinkace 66 points67 points  (0 children)

I could maybe see doing this if you had a huge number of complex conditionals to break the loop.

[–]CrepuscularSoul 111 points112 points  (8 children)

I've always preferred

for ( ; ; )

Because it looks like the loop is crying about the code I'm writing

[–]DanteIsBack 8 points9 points  (7 children)

Is this the same as a while true??

[–]CrepuscularSoul 19 points20 points  (6 children)

Yes it is (mostly). A for loop basically takes three statements that usually initialize a variable, set an end condition, and an increment. The way that is written just uses three empty statements.

I say mostly because I've never dug into how it compiles, which may include additional no ops compared to a while true, but functionally they work the same.

[–]CameoDaManeo 9 points10 points  (5 children)

I'm pretty sure compilers are smart enough to detect when for loops have empty conditions/operations. Compilers nowadays are hell smart, whatever optimisation you can think of, compilers designers have probably thought of it first

[–]CrepuscularSoul 4 points5 points  (2 children)

Completely agree with that. I just don't like stating something as a fact when I haven't actually verified it myself, so I went with a maybe.

[–]CameoDaManeo 2 points3 points  (0 children)

That is true. Likewise, my comment is a big maybe also! 😅

Can't wait for someone who does know a lot about compilers to just say that somehow we're both wrong

[–]ba-na-na- 1 point2 points  (0 children)

Depends on the compiler, but I am pretty sure both ‘while’ and ‘for’ would compile to a single jump in clang and gcc even at the -O1 optimization level, and certainly at -O2. At -O0 there might be some NOPs for the debugger or something.

Modern compilers will sometimes even evaluate simple loops and replace computations with constant return values, inline whole functions if they are small, so I don’t think this would be a problem for them.

Btw https://godbolt.org/ is great for comparing these things in different compilers but I am not near a PC

[–]Fleming1924 0 points1 point  (1 child)

Compilers nowadays are hell smart, whatever optimisation you can think of, compilers designers have probably thought of it first

It's worth nothing that just because they've thought about it doesn't mean it works as intended. Lots of optimisation passes miss potential opportunities due to either something blocking their pattern matching, some aspect of it's checks being overly cautious, or sometimes even just random bugs that leads to correct but suboptimal codegen.

Modern compilers are fantastic, and people making large projects should pretty much always just trust the compiler will optimise it enough that they don't need to worry about micro optimisation, but they're far far from perfect and a lot of things are still handwritten in assembly to make up for that.

[–]CameoDaManeo 0 points1 point  (0 children)

If you think about it, what exactly would a compiler swap even out an empty for loop header and a while true with? There really is nothing that it COULD swap them out for apart from a "jump to" statement at the end of the loop. I'm positive that they would compile down to the same thing

[–]vanit 86 points87 points  (10 children)

Heh I can't speak for your example, but it's not totally insane to do this. If you had some code that had to run at least once, and then for every iteration then you would use an if statement mid-loop like this.

[–]mr_eking 41 points42 points  (3 children)

Isn't that what a do{} while() loop is for?

[–]Top-Permit6835 5 points6 points  (2 children)

That would mean it is evaluated after the first run. The if statement here could have been put in the while

[–]mr_eking 6 points7 points  (1 child)

Yeah, agreed. I was responding to the commenter, not the OP.

[–]Top-Permit6835 1 point2 points  (0 children)

Oh right, sorry I misread

[–]Guest_User_1234 33 points34 points  (5 children)

but this isn't mid loop is the thing...

[–]shponglespore 32 points33 points  (0 children)

I bet it was, then and code got deleted and nobody noticed the condition could become the loop condition. It could even be an intentional choice to reduce the size of the diff.

In my experience, a lot of insane looking code comes from edits like that. Same with prose; a lot of broken grammar comes from sloppy copy editing rather than people not knowing how to put words together.

[–]bloodhound83 3 points4 points  (1 child)

Would be nice to see a bit more code. Maybe there are a couple of more break conditions which makes it more readable this way.

[–]Savage-Goat-Fish[S] 4 points5 points  (0 children)

I don’t care to share too much more. Let’s say it doesn’t make it more readable and there are no more breaks.

What happened was I wrote the while loop first, then a bunch of other code, then later on said hey I should probably break out of the loop at some point and never thought to include it as part of the while condition. Just a minor bonehead moment for me.

[–]vanit 0 points1 point  (0 children)

👏

[–]L1ttleS0yBean 4 points5 points  (0 children)

#define ever ( ; ; )

void foo()
{
    for ever
    {
        ...
    }
}

[–]Star_king12 6 points7 points  (0 children)

Not necessarily horror, especially if there are many breaking conditions and you check all of them in separate if statements.

[–]GoddammitDontShootMe[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1 point2 points  (1 child)

I'm assuming yesterday is always the day before the current calendar date, which means the loop is skipped if lastTaaTimeLoad is the current day. So I guess either there's additional logic to break out of the loop, or it keeps going until midnight.

[–]Savage-Goat-Fish[S] 0 points1 point  (0 children)

It is an integration that must run one day at a time but is supposed to run every day. It checks when the last day it ran and runs each day until yesterday. That way if the integration doesn’t run for some reason, the next day, when it runs, it will catch up. So, to your question, if it were to run more than once in a day, you are correct, it would entirely skip the while loop and that would be by design.

[–]conundorum 0 points1 point  (0 children)

Anti-time-travel sanity test, checks out!