On that, if youâre referring to the try/except way or the single-use functions way, I think we just disagree on whatâs readable and whatâs not. If youâre not, please introduce that other workaround.
The try/except adds indenting, which in the middle of several concentric loops, doesnât help readability. It also requires defining single-use exceptions, which pollute the namespace, and are not particularly clear to understand what their purpose is. Itâs the same weird kind of non-error exception as StopIteration : itâs sensible, sure, but not simple to understand.
The single-use function moves execution to another part of the code, which makes it harder to follow than if execution were just kept in the loopsâ block. And, coming to think of it, I think that you need to define several single-use functions to emulate my example where you need to break to two different loop levels in a three-loop structure.
I donât see how these refactoring could be considered more readable and/or maintainable (more about that further down) than the multi-break. I also donât see how labeled breaks (for example) can harm maintainability.
@Rosuav ok, I understand your argument. I think this is necessary to make reasonably readable code, even if the circumstances when itâs needed are rare. But for example if the feature induces a performance loss to compile any loop, the fact that itâs barely used and there are (dirty) workarounds is very sensible. And of course the implementation cost is to be taken into account, too.
Iâll try to find real examples in code I worked on.
But I think there is another advantage for multi-breaks wrt maintainability which wasnât considered thus far, and which may answer your question. Imagine in your code you have a simple loop, where you parse the lines of a file, and you break when some condition is verified on the considered line.
Now, say thereâs a project update, some new feature, requiring you to parse several files, which turns your single loop into two loops, inside one another. You could simply add the for file in files as outerloop line and indent the existing loop, and turn the break into a break outerloop. The git diff would be very understandable, just 4 green spaces per line, one new line, and one altered break statement.
If you need to go back and forth between these two versions of the code, if youâre not sure, or if youâre doing it in some other branch and you need to maintain both in parallel (whatever) the codes are very similar, thereâs only one edited line, one added line, and a bit more indenting.
However, if you use the single-use function way, you need to go back and forth between a version with a simple for loop, and a version which calls a function defined in another part of the code, where the original loop was moved to. That function contains two loops, and the break has been turned into a return, which doesnât help recognizing itâs the original code that has been moved (that adds more time when checking the commit to see if it adds a bug or contains a typo).
And, you need to figure out which variables you need in the loops, because you will have to pass them to the function, and what information you need to get out of the loop. That isnât necessary in the multi-break version, so that means itâs easier to upgrade your code to the multi-break version, than to upgrade it to the single-use function version.