|
|
Subscribe / Log in / New account

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

> Millionaire thought that requiring refactoring into a function was less than ideal.

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.


to post comments

Python multi-level break and continue

Posted Sep 1, 2022 12:42 UTC (Thu) by eru (subscriber, #2753) [Link] (1 responses)

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!

As a contribution to the fun syntax bikeshedding, a language I was involved in implementing had a loop roughly like this:

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

Posted Sep 1, 2022 21:00 UTC (Thu) by bartoc (guest, #124262) [Link]

Strongly agree with this take. If you aren't going to reuse it then it's usually not worth splitting stuff out it's own function. Just let me read the darn code. If the function is doing some well known algorithm maybe it _is_ worth it, but even then sometimes a comment is just as good as a function.

Keeping things inline can make optimization opportunities more clear and make anti-optimizations stick out more, as well.

Python multi-level break and continue

Posted Sep 1, 2022 23:48 UTC (Thu) by excors (subscriber, #95769) [Link]

I find splitting into smaller functions (just so you can replace the multi-level break with a return) is the optimal solution in maybe 95% of cases. But in the other 5% it can be very awkward, usually because there are several variables shared between the outermost and innermost code and you'd have to explicitly pass them all across the new function boundary (particularly annoying if you've given them nice long descriptive names, which you now have to duplicate into the function definition and the function call), or (even more rarely) because you've already split it once and if you add another function layer then 'return' won't return far enough.

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.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds