4 ms·
> Sure, and break/continue etc. should be discouraged for that precise reason. If you naively banish break and continue, but then the programmer has to work ar
by kazinator 8y ago
> Sure, and break/continue etc. should be discouraged for that precise reason.
If you naively banish break and continue, but then the programmer has to work around your banishment by adding one more state variable, and assigning to that variable, you have not gained anything.
You've pushed down a bulge in the carpet, only to have it pop up elsewhere.
Assignment and goto are bedfellows, which is why purely functional programming banishes both. (Goto, at its essence, is in fact an assignment to the program counter.)
- contravariant 8y agoNote I said discouraged not banished.
- joshuamorton 8y agoGenerally speaking, things that use break can be cleaned up by using python's `any` or `all` constructs over a slightly reworded loop. This isn't always true, but handles most of the common cases. I don't think that I've yet come across a use of break/continue that can't be pretty cleanly refactored into a helper method like def my_filter(items): for x in items: if cond(x): yield x for x in my_filter(base_list): process(x) This ends up being pretty clean and describes exactly what/why you're filtering things, plus as the conditions change you can test things individually.
- maxbond 8y agoOne might also consider using something like `map(process, filter(cond, items))`