Re: regarding "goto" in C



Chris Dollin wrote:
websnarf@xxxxxxxxx wrote:


I personally follow only one rule regarding goto: avoid using it unless
using it leads to either clearer code or faster (executing) code.  This
rule alone is sufficient to make the use of goto fairly uncommon in my
code to the point of "spaghetti code" never being an issue.  (If
there's one exception it would have to be parsers -- but in those cases
even the cleanest control structures don't actually lead to a clearer
representation of the parser.)


I've never had to use a goto in a parser; could you unpack the reasons
why you've wanted one? [email if it's likely to be wildly off-topic.]


As a *troll* I use goto often to increase execution speed, easing code readability mostly in library utilities that I write once and never look at again.


Here is my general structure.

int func(arguments)
{
   int error = 0;

   /* lengthy function code listing */

   /* at various "test" points in function the test below exists */
   if (something_aint_right)
   {
       error = 1;
       goto FINAL_func;
   }

FINAL_func:
   /* clean up any local resources, like malloc() calls, device opens, etc */

   if (error)
   {
      /* any aditional code for error processing here. */
   }

   return(error);
}


This technique simple allows me to not code multiple in line error condition tests, and give me the ability to immediately jump out when things go bad.
.




Relevant Pages

  • Re: regarding "goto" in C
    ... > I personally follow only one rule regarding goto: ... > using it leads to either clearer code or faster code. ... I've never had to use a goto in a parser; ...
    (comp.lang.c)
  • Re: Iterate before loop end
    ... >> I did consider GoTo, just for a moment, but you know what that leads ... >Yeah, in this instance, an optimal solution...no overhead of extra ...
    (microsoft.public.vb.general.discussion)
  • Re: Iterate before loop end
    ... > I did consider GoTo, just for a moment, but you know what that leads ... Yeah, in this instance, an optimal solution...no overhead of extra ...
    (microsoft.public.vb.general.discussion)
  • question re: goto statement
    ... I have heard that it is frowned upon, to use a goto statement, on the ... grounds that it leads to "spaghetti code." ...
    (alt.comp.lang.learn.c-cpp)