• 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: Yuhui <email@hidden>
  • Date: Wed, 6 Aug 2003 17:04:45 +0800

Alastair,

While I can understand the use for a goto, I've learned in my experience that most goto's can be replaced with methods. I think there's more flexibility with methods because you can more easily "expand" them to accomodate bug fixes or add new features/functions. Also, if there are several goto statements in different parts of the program that use a particular block of code, then methods would reduce redundancy.

E.g. in (b), instead of "goto errexit", I could say "errexit()". The same would apply for (c).

(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)).
_______________________________________________
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: Thomas Harrington <email@hidden>
    • Re: break, continue, goto
      • From: "Alastair J.Houghton" <email@hidden>
  • Prev by Date: Re: getting dragged file's name
  • Next by Date: NSToolbarItem tag
  • Previous by thread: Re: break, continue, goto
  • Next by thread: Re: break, continue, goto
  • Index(es):
    • Date
    • Thread