Re: break bug
- Posted by DerekParnell (admin) Nov 10, 2012
- 1214 views
Sorry... should have used exit!
Better yet, goto would have been even cleaner!
Well, that's one opinion
I think that idea of 'goto is evil' has got it wrong, because it's not the jump that is the problem but the label. What I mean is that when one sees goto SomeLabel in code, it's not so hard to follow where the control logic flows to. But when one sees label SomeLabel in code, there can be a lot of needless work to find out all the possible ways that a program can get to that label.
The main aim of both break and exit is to eliminate excessive labels. And even when you need a label on those statements, what Euphoria does is not to name the destination of a jump, but names the construct that one is trying to leave. This significantly reduces the scope of a jump to something that is manageable for a human reader of code.
Now, when it comes to the difference between break and exit, the development team had lots of heated discussion about both the need for two statements and also what those statements would be. The exit statement was a given, seeing that it was already well established in Euphoria as the way to prematurely leave a loop construct. To change this would cause too much existing code to fail.
With the introduction of the switch construct, when fallthru was in effect, we needed a way to allow some code to leave the switch rather than automatically fall through to the next statement. If we had of used exit, it would have caused ambiguity when a switch was inside a loop or visa versa - are we trying to leave the switch or leave the loop? We could have resolved this by using the exit label form but that could have resulted in a lot more typing and/or accidentally introduced bugs (when the label phrase was forgotten to be added).
Another idea was to use exit if and exit switch, but this might have made parsing some code a problem. (I can't see it myself but I was in the minority on that one).
The break statement was already in use by a few languages for this purpose, so it was decided to continue with this idea in Euphoria. The use of break should be very rare in Euphoria because fallthru is rare in switches and breaking out of a nested if statement is also a rare event.