Python multi-level break and continue
Python multi-level break and continue
Posted Sep 1, 2022 12:01 UTC (Thu) by jezuch (subscriber, #52988)Parent article: Python multi-level break and continue
In my 20+ years of software engineering experience I find this to be the optimal solution to the "problem". Splitting code into smaller functions is generally considered a Good Practice.
Labeled break seems like a useful(*) feature, but it's almost always an indication that the code is due for some refactoring. And it's a huge red flag sure to be drawing unwanted attention of predators (code reviewers) :)
(*) it also feels "neat". I suppose that's 80% of the reason our geek brains want it.
Posted Sep 1, 2022 12:42 UTC (Thu)
by eru (subscriber, #2753)
[Link] (1 responses)
As a contribution to the fun syntax bikeshedding, a language I was involved in implementing had a loop roughly like this:
Posted Sep 1, 2022 21:00 UTC (Thu)
by bartoc (guest, #124262)
[Link]
Keeping things inline can make optimization opportunities more clear and make anti-optimizations stick out more, as well.
Posted Sep 1, 2022 23:48 UTC (Thu)
by excors (subscriber, #95769)
[Link]
In those cases the code can be simpler and more clearly express the programmer's intent by using a multi-level break instead. They're not very common cases and the workarounds aren't too bad, so it's not a crucial language feature (as demonstrated by the successful languages without it), but it's also not a feature that seems to be widely abused or that causes great confusion (as demonstrated by the successful languages with it), so I think on balance it's still nicer if the language gives you the option.
Splitting code into smaller functions is often good practice, but not always. I often find it clearer to have the whole inline, so it can be seen at a glance, instead of lots of small functions used only once. The latter is like reading a text that keeps jumping into footnotes!
Python multi-level break and continue
while cond1
while cond2
...
// Want to break out of both loop levels?
if satisfied
break foundIt
endwhile
endwhile foundIt
The break is a jump forward, so it makes sense the label is at the endwhile. Of course this wont work too well in Python that lacks a delimiter to mark the end of the loop, other than decreasing indentation.
Python multi-level break and continue
Python multi-level break and continue
