• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: break, continue, goto
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: break, continue, goto


  • Subject: Re: break, continue, goto
  • From: "Alastair J.Houghton" <email@hidden>
  • Date: Mon, 4 Aug 2003 12:45:39 +0100

On Monday, August 4, 2003, at 11:04 am, Stephan Lichtenauer wrote:

David,

I think (and that is of course just my personal opinion, although I think it is not too unusual...) you never need a goto. If you need one, you are doing something wrong and you should rethink and reorder your code into an appropriate loop. Some languages even do not offer a goto and they nevertheless work well.

Languages that don't offer goto generally have a few other control flow facilities that C doesn't have (for example, loop labelling and exceptions). Rather than the oft-quoted "thou shalt not use goto", I personally prefer to leave it up to common sense; for example, IMO the following situations are (or may be) best addressed by the use of goto:

(a) When you want to break out of a set of nested loops/switch statements (rather than just the inner one). The main alternative (flags, tests and breaks) is messy, although it *is* sometimes possible to make the set of loops into a separate function and use "return" (however, it is arguable whether that is any clearer than the use of goto).

(b) When you need to provide a standard set of error handling/clean-up code for the duration of a function body. In this situation, you are using goto like a C++ throw; e.g.

void *myFunction(void) {
void *buffer = malloc (65536);

if (!buffer)
return NULL;

if (doSomethingWithBuffer() < 0)
goto errexit;

if (doSomethingElseWithBuffer() < 0)
goto errexit;

return buffer;

errexit:

free (buffer);
return NULL;
}

This is a *lot* clearer than writing the error handling/clean-up code over and over again, and there really isn't any better way to do this with plain C.

(c) When you have a loop that is searching for something, it can *sometimes* be clearer to write something like

while (someFunction()) {
if (thing == search_value)
goto found;
}

return 0;

found:

as opposed to

int found = 0;

while (someFunction()) {
if (thing == search_value) {
found = 1;
break;
}
}

if (!found)
return 0;

(Which is better is a more difficult decision to make than situations (a) and (b)).

(c) In generated code. There's no reason that computer-generated code shouldn't have spaghetti in it, because you can always look at the input (which presumably isn't spaghetti), and spaghetti can be faster for some applications (as well as being easier to generate under some circumstances). A good example of this type of thing is the output of the Bison parser generator.

===

I am certainly not claiming that goto is appropriate as a general flow control construct in your program... rather that sometimes it may be the best construct available for a particular purpose and the (IMO silly) stigma attached to its use shouldn't stop you from using it in those circumstances.

Kind regards,

Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: break, continue, goto
      • From: Stephan Lichtenauer <email@hidden>
References: 
 >Re: break, continue, goto (From: Stephan Lichtenauer <email@hidden>)

  • Prev by Date: Re: break, continue, goto
  • Next by Date: Re: break, continue, goto
  • Previous by thread: Re: break, continue, goto
  • Next by thread: Re: break, continue, goto
  • Index(es):
    • Date
    • Thread