Re: Simple question!




"Michael K. O'Neill" <MikeAThon2000@xxxxxxxxxxxxxxxxxx> a écrit dans le
message de news: %2368VlTioGHA.4240@xxxxxxxxxxxxxxxxxxxxxxx

Do not use gotos.... ever



For goodness sake...

The use of goto's is generally harmful and often leads to terrible,
unstructured code. Its use should be usually be avoided.

But there are plenty of circumstances when a goto is better. The
generalized
"avoid goto's" rule doesn't mean that goto's should never be used, and
it's
silly to impose pedagogical one-size-fits-all rules -- such as "Do not use
gotos.... ever" -- to all situations.

Nonetheless, an introductory book that presents goto wihtout warning about
it's dangerosity deserves the recycle bin IMHO.

There are a few widely-recognized
situations where a goto is perfectly acceptable and maybe superior.
In C, I would tend to agree; But in C++? I don't think so....

One is
when trying to exit from a deep loop.
Break your loops into functions and use returns. Having too deeply nested
loops inside one function is sign of a bad design anyway...

Another is to consolidate all
error-cleanup in one spot, to avoid duplicated code.
Use exceptions, that's why they where invented. Also, if used properly,
exceptions allow for much easier ressources management in case of error, so
that there is no need for error-cleanup (it is taken care of by destructors)

Recent object-oriented languages (that provide exceptions facilty) such as
C# and Java have dismissed goto altogether : their conceptors felt that the
few cases where it may be helpfull were no match for the risk of
ill-structured, unmaintenable, code.

Arnaud
MVP - VC


.



Relevant Pages

  • Re: What do you think about the code?
    ... Well that's team democracy at work. ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ...
    (comp.lang.c)
  • Re: goto
    ... > But the same is true about blanket forbidding of goto. ... > appropriate because the 'cleanup' code would need to be duplicated which ... I'd also bet that there is significant duplication within the class ... If we have to do 3 nested for loops here, ...
    (comp.lang.cpp)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Within a fixed scope, but you still identify the error handler (the ...
    (comp.lang.c)
  • Re: Java needs "goto" (was Re: hi)
    ... Assemblers need "goto" but no decent HLL does. ... I don't use this construction in C either and, again, don't need it. ... Yes - but that's a result of classes that throw exceptions when their methods would be better off returning control values. ... martin@ | Martin Gregorie ...
    (comp.lang.java.programmer)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Anything allocated on the stack will get freed when longjmp gets ...
    (comp.lang.c)