3 ms·
> But here’s the reality: most code is not complex enough to warrant while loops, and most while loops are better written as for loops. Hard disagree. Obviousl
by dragonwriter 5y ago
> But here’s the reality: most code is not complex enough to warrant while loops, and most while loops are better written as for loops.
Hard disagree. Obviously, if the loop is a fixed number of iterations or iteration over a collection (which is trivial to identify by human inspection but probably not for a linter), a for loop is better but it is not uncommon to be looping until a (possibly compound) condition becomes true that cannot be expressed as a fixed number of iterations or (at least naturally; you can embed arbitrary complexity into a generator, though usually, absent unbounded recursion, that will just push the “while” down behind a layer of abstraction) as exhaustion of an iterable.
- joatmon-snoo 5y agoI've actually found that there are a number of situations where I really want: while True: ... if condition: break ... and kinda wish there was a nicer syntax for this.
- ohazi 5y agoRust has `loop {}` which I thought was weird at first, but also ended up use more than I would have expected.
- kevinmgranger 5y agoOne especially nice part of rust's `loop` is since it's guaranteed to run at least once and to terminate, you can break with a value! let foo = loop { // ... if condition { break value; } // ... }
- dragonwriter 5y agoA Ruby-like “until” or Pascal “repeat...until” (C’s do...while is equivalent but less clear choice of keywords, IMO.) The Pascal form wouldn't really fit Python's indentation-without-closing-marker block syntax preference, but the Ruby version would. If it existed, I’d be tempted to suggest a linter rule that prohibits break inside an until (it should only be used when the main exit condition is the only exit condition.)
- dragonwriter 5y agoToo late to correct, but I can tell I haven't been using Ruby much for a while; Ruby until is just negated while, like C it uses while (or until!) at the end for Pascal-style repeat...until logic.
- tech2 5y agoAnd this is where I really miss Sather: loop while!(condition); ... end; for while, versus loop ... while!(condition); end; for do-while, and loop ... while!(condition); ... end; in place of: while True: ... if not condition: break ... Alas, it never really took off.
- dkersten 5y agoIn my personal experience, its quite rare outside of a few specific cases where I need unbounded loops. The specific cases where I do need them tend to be event loops -- a game loop, a consume the queue loop, a read and process input loop, etc, basically waiting on some external (to that code, not necessarily to the program) data, forever until the program is told to stop. Sometimes they're provided for me by some library or framework, sometimes I need to write them myself. Almost every other case, in my own code at least, is a bounded loop. Usually processing a collection, but also often processing bounded ranges.