> with all the jumping forth and back not caring for scope and everything.
> a code with a goto is usually dead code.
Use of scopes is completely orthogonal to the use of goto. A programmer who would use a scope if and only if a syntactic rule forces one to do so is a poor programmer - no matter that the eschewal of goto is zealously adhered to with religious fervour.
> "goto" is such a dangerous construct, and can have such a negative impact on maintainability
> goto is a terrible thing to use.
Absolute balderdash.
If goto is really a terrible thing to use, no matter how it is used, be certain that it would have become a deprecated feature of the language.
This:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
do // read_next_item:
{
// ...
if( ... ) continue ; // goto read_next_item
// ...
if( ... ) break ; // goto no_more_items
// ...
} while( ... ) ; // if( ... ) goto read_next_item
// no_more_items:
do_something_with_the_items() ;
|
is not any more readable or maintainable than this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
read_next_item: // do
{
// ...
if( ... ) goto read_next_item ; // continue
// ...
if( ... ) goto no_more_items ; // break
// ...
if( ... ) goto read_next_item ; // while( ... )
}
no_more_items:
do_something_with_the_items() ;
|
Have nested loops, with a need to break out of some of them, and the version going through grotesque contortions to avoid that one
goto
somehow, anyhow, would be likely to be less readable and less maintainable.
Grey Wolf wrote: |
---|
goto is not bad it is just misused. Learn what it does and how to use it. There is often a better way than using goto but sometimes you will find that it is a simple way to achieve something. |
Grey Wolf: +1
People who not know how to use a particular programming construct correctly would be well advised not use it; not till they have educated themselves. That is as true of goto as it is for (say) virtual inheritance.
> the situation is simple: use goto as much as you want,
> but keep in mind that it makes code harder to read.
Not necessarily. It can actually make the code easier to read.
From the post that TheIdeasMan had linked to:
Would this real-life (so I believe, it was on the internet, in a discussion of goto) C code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int parse()
{
Token tok ;
reading:
tok = gettoken() ;
if( tok == END ) return ACCEPT ;
shifting:
if( shift(tok) ) goto reading ;
reducing:
if( reduce(tok) ) goto shifting ;
return ERROR ;
}
|
become simpler or more elegant if we banished the gotos? |