Re:Loop Exits and Structured Programming: Reopening the Deba...
by sedachv on Monday June 12, 2017 @10:47PM
(#14542571)
Labelled break is strictly less powerful than gotos, and IMO its implementation in Java is far inferior to the block Common Lisp special form (https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node85.html) because s-expressions make the lexical scoping unambiguously visible.
IMO there is no good substitute for gotos when writing transpilers (and certain interpreters) and low-level concurrency code. The Common Lisp prog macro is the best way that I have seen to use gotos (https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node91.html).
Re:Loop Exits and Structured Programming: Reopening the Deba...
by dbaupp on Tuesday June 13, 2017 @12:11AM
(#14542813)
Of course it is strictly less powerful than gotos. That's actually the point of the more structured constructs: they are more restrictive and so can be reasoned about more easily. Note that I'm specifically piking up on labelled break/continue as a substitute for nested loops like the following
while ... {
bool shouldBreakOuter = false;
while ... { ... shouldBreakOuter = true; break; }
if (shouldBreakOuter) break;
}
since that what the original comments description sounded like. Being a rather general control flow mechanism, there are of course other uses of goto which this doesn't cover.That said, I'm not sure I understand the comparison: the lexical scoping doesn't seem to be ambiguous in Java. Could you expand on what you mean?
Re:Loop Exits and Structured Programming: Reopening the Deba...
by sedachv on Tuesday June 13, 2017 @01:34AM
(#14543025)
If you visualize nested scopes as indent levels, the labelXYZ: sits in an indent level above where it is visible. Not only that, but labelXYZ: does not come before the statement that it transfers control to, but two statements before. If all that was not bad enough, most people format the labeled statement to sit at the same indentation level. The combination of these three things is pretty much the worst possible when it comes to reading code. Compare this to block/tagbody/prog and their indentation conventions in Common Lisp.
Re:Loop Exits and Structured Programming: Reopening the Deba...
by userbinator on Monday June 12, 2017 @10:27PM
(#14542518)
Unfortunately, in my experience the abuse of labelled breaks to avoid gotos tends to be even worse than abuse of regular breaks, since the label no longer shows immediately where the flow goes; it only labels the start of the block, when what you really want to see is the end.
Re:Loop Exits and Structured Programming: Reopening the Deba...
by dbaupp on Tuesday June 13, 2017 @12:14AM
(#14542823)
I guess that could indeed be annoying for reading code, especially if the labelled block/loop is very long (I personally usually end up using these for short nested loops, longer loops usually get broken out into separate functions, and the biggest teams/programs I've worked on haven't been using languages that have this feature). That said, the extra structure/more restrictions of labelled breaks lends itself to compilers and other tooling being more helpful, both for optimisations (although SSA + basic blocks that most compilers now use makes this somewhat irrelevant) and for catching mistakes while, for instance, refactoring.
In any case, I strongly disagree that labelled breaks aren't better than regular breaks: the chains of booleans required otherwise are very annoying and error prone.
Re:Loop Exits and Structured Programming: Reopening the Deba...
by nickpsecurity on Monday June 12, 2017 @10:19PM
(#14542495)
It's why some of us built 4GL's, DSL's, preprocessors, and so on that added stuff to such languages but extracted to purely that language. Mine was a BASIC with macros translated to C w/ safety checks.
Of course, thinking about labelled break is just dreaming when required/choosing to work in a language without it.