4 ms·
Wow. Insane that he lost his bonus for being a good lead dev. Some dev had put the classic `<=` instead of `<` in a for loop check and the for loop body prompt
by rowanG077 3y ago
Wow. Insane that he lost his bonus for being a good lead dev.
Some dev had put the classic `<=` instead of `<` in a for loop check and the for loop body promptly corrupted some memory leading to heisencrashes. It took weeks to narrow down with 10+ hour days. Once they did and fixed it the boss asked for a name who had written the offending code. Timothy did not bend. Boss was salty and cut his bonus.
- zabzonk 3y agowhenever i see '<=' in code, my bug detectors start twitching. to begin with, doing effectively two tests in one always seems to over-complicate things.
- symlinkk 3y agoSeriously?
- zabzonk 3y agoyep - i am not saying there are no cases for using it, but use of it is always worth looking at.
- CamperBob2 3y agoSame whenever I see a do...while loop. Nothing wrong with a do...while loop, but I've learned to look twice when crossing that particular street. Most loops that don't have bugs tend to support zero iterations seamlessly and inherently.
- dllthomas 3y agoIf you squint, aren't those the same case?
- zabzonk 3y agoum, a do...while always does something at least once - a for-loop not.
- dllthomas 3y agoI was trying to say that the difference between a for loop with < and a for loop with <= is roughly the same as the difference between a while loop and a do-while loop, and to whatever degree that's true they might be due suspicion for the same reason.
- jonhohle 3y agoI agree with parent. do-while communicates that a thing will always execute once, and maybe more, and a while or for loop (even in the higher level for-each/for-in case) may never execute at all. Being able to choose the right control structure (and data structures) can provide additional information to the correctness of the code than arbitrary choices which may perform the same function. Choosing the Most expressive options may or may not conflict with performance, in which case, comments are helpful to describe why more obvious approaches weren’t taken.
- dllthomas 3y agozabzonk is obviously correct about the behavior of the control structures - that's not something I ever meant to disagree about. The thread had had one poster state they find <= suspicious in a for loop, and another about any do-while loop. I don't think these are literally the same thing (hence "if you squint") but I think they share a meaningful similarity of adding an extra pass through the loop compared to the other form and I thought that was interesting.
- derefr 3y agox <= y isn't two tests. In a low-level sense, branch-if-greater-or-equal is one instruction even on RISC machines — and not one any more "complex" than branch-if-greater. In an abstract boolean-algebra sense, x <= y is the same as !(x > y).
- zabzonk 3y ago> x <= y is the same as !(x > y) i.e. two tests, or perhaps i should say logical operations. i'm not saying don't use '<=', but do check that it is doing what you think it should.
- derefr 3y agoLet me put it another way: the following are the same code: if: x <= y, do: foo(), else: nothing() if: y < x, do: nothing(), else: foo()
- AugurCognito 3y agoShouldn't the last statement be: if: y > x, do: nothing(), else: foo()
- rurban 3y agoNope, order of evaluation might matter with impure code.
- rowanG077 3y agoHow exactly? Well there is an extreme case where x and y are volatile but even then the order is implementation defined so I can't see a world where it matters.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- rgbgraph 3y agoThis form is basically always used for enumeration. Might as well just do (x != y); unless you're specifically looking to guard against erroneous bitflips that would be caught by the prior (x < y), but not the former. But at that point -- it's likely your result is going to be garbage. In essence they're the same; but one you never have to think about whether you're using it correctly. Can anyone find a reason not to swap out (x < y) for (x != y)?
- rowanG077 3y ago`<` communicates the intent way better. It also more readable Example: The loop should run as long as the counter is smaller than 10. `i /= 10` achieves the same thing but communicates something very different. It implies `i` may be changed inside the loop to become larger than 10. The condition itself implies a larger valid state space. It's a moot point anyway. Modern languages have much better looping constructs.
- olkingcole 3y ago(If I'm understanding correctly) one example is if have two places where you x++ in the loop, so if you make an error in there you might end up hitting both of them and incrementing from y-1 to y+1.
- urthor 3y ago[flagged]
- rincebrain 3y agoIf you have a bug and produce x+=2, you never terminate? One might argue corrupting much larger swathes of memory is better from a "fail-fast" perspective, but there's always tradeoffs.