webJose wrote: |
---|
Never heard about such argument before. All I have ever heard about goto is bad things, one very specific approved case of use and some hard-to-believe statement I'd rather not mention here. All that you heard is nonsense to me. |
It's strange you've never heard such an argument before, as it has some very good points! Sometimes goto is appropriate, although rarely. And a lot of the time people misuse control structures, which could be replaced by a simpler form using the control structures a different way (or by goto statements, but it's unlikely that would make it simpler).
Of course you've heard many bad things about goto, though; it's 99.6% of the time a terrible idea in well-written code!
Disch wrote: |
---|
More traditional {block} approaches are quickly identifiable. Once you see a { you can quickly notice the matching } to know what all code is affected. You also can see that there's a clear entry point, and what kind of block it is (if/for/while/etc)
With goto you can't see any of that. You can't know what section of the code will jump to a label without reading each and every line of code in the function. There are no clear boundaries to the affected code, because there are no boundaries for goto. |
Yes, that is of course why control structures are used. They provide a similar kind of functionality to what was already used all the time in programs with jump statements, and clearly show program flow compared with goto and labels.
Ben Bowen wrote: |
---|
"Spaghetti code." Yes, but the argument was about how our given control structures are also capable of rendering spaghetti structure alike the goto statement. |
As I said above, I totally agree it's easy to make bad code with control structures, although easier with goto statements.
naraku9333 wrote: |
---|
I disagree, the control structures do exactly as the name implies and help control the structure of the code. They have a single entry point and exit and flow top down. |
I think they help, but they can be misused; I believe you're wrong in saying that just because something is effective it means it can't be used incorrectly to disadvantage!
chrisname wrote: |
---|
I agree that goto's use should be restricted, but I don't agree that it should never be used. There are some cases when an unconditional jump is better than an alternative; for example, if you need to exit a function from several different locations and do some common clean-up (release memory and such) before exiting, the best way to do that is to use something like this:
... |
I do the same thing in my code a lot, too! You could easily do it in the control structures too, but it makes it look more complicated, in my opinion, and harder to understand. This is a very nice example of where goto is appropriate (although this code is so simple and unrestrictive, it could in fact be done with more appropriate control structure, but I imagine it 5 times as long, and with the freeing of resources as an inline function).
EDIT: It seems everyone here agrees that goto is a bad idea most of the time, but some people would say it's not ever worth using and some would say it can be cleaner than control structures at points...