Re: Using "break" to Break Out of Multiple Loops in One Call
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Fri, 15 Feb 2008 16:55:27 -0800
On Fri, 15 Feb 2008 16:30:01 -0800, David Anton <DavidAnton@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I've always considered Java 'labelled breaks' as just a way for the Java
designers to get 'goto' into Java without admitting defeat.
I agree with you, sort of.
Especially when
you consider that you can use the labelled break to jump out of from any
braced block, not just loops.
The main difference I see is that the syntax of the Java labeled break reinforces the idea of a structured flow. It's true that you can add braces to force an equivalence to a goto, but when you do so the language is (if you're paying attention) giving you very loud clues about whether or not that's an appropriate use of the "break-as-goto".
Personally, I'm not of the mind that "goto" statements are inherently bad. They certainly can be misused, and may well be misused much more than other language constructs. But they also have their place (and I don't just mean as a replacement for falling through in "case" statements :) ). I have found that, as I explore Java and implement real code using it, that in a situation where I might have used a "goto", using a labeled "break" provides similar functionality, but does so in a way that I think makes the structure of the code clearer.
I prefer to believe that I never misuse a "goto" statement, but whether I do or not, it's my belief that the Java syntax would make it a lot more obvious to me if I'd abused the statement.
So, I'm not sure that in Java it was so much a matter of whether they were admitting defeat or not, as it is a matter of trying to provide a slightly different syntax to do the same thing, but in a more clear way that avoids code maintenance issues that misuse of gotos can cause.
Granted, I don't know what the Java designers were actually thinking, but I like to think that's at least part of why it's the way it is.
Of course, it begs the question: why isn't a similar syntax in C#? After all, my after-the-fact impression of C#, having made the transition backwards into Java from C#, is that C# seems to try to take what was best of Java, while avoiding some of the more confusing, redundant, or restrictive parts. Inasmuch as I personally find the labeled "break" statements somewhat clearer than a "goto" statement, that doesn't exactly fit into the impression I've formed.
But then, again, that's just my impression. I don't really know all of the motivations behind the language design choices, and maybe they felt they had a good reason to return back to the conventional "goto" statement.
Either way, I don't think there's anything inherently bad about labeled "break" statements or "goto" statements. Sometimes they are just what you need. Just remember not to aim at your foot when using them. :)
Pete
.
- Follow-Ups:
- Re: Using "break" to Break Out of Multiple Loops in One Call
- From: Scott Roberts
- Re: Using "break" to Break Out of Multiple Loops in One Call
- From: Donkey Hot
- Re: Using "break" to Break Out of Multiple Loops in One Call
- References:
- Using "break" to Break Out of Multiple Loops in One Call
- From: O.B.
- Re: Using "break" to Break Out of Multiple Loops in One Call
- From: Jeroen Mostert
- Using "break" to Break Out of Multiple Loops in One Call
- Prev by Date: Editor to read application configuration
- Next by Date: Re: Using "break" to Break Out of Multiple Loops in One Call
- Previous by thread: Re: Using "break" to Break Out of Multiple Loops in One Call
- Next by thread: Re: Using "break" to Break Out of Multiple Loops in One Call
- Index(es):
Relevant Pages
|