why are GOTO's not used ...

Discussion in 'C Programming' started by raashid bhatt, Aug 17, 2008.

  1. why are GOTO's not used they just a simple JMP instructions what's bad
    about them
    raashid bhatt, Aug 17, 2008
    #1
    1. Advertising

  2. On 17 Aug, 21:13, (Richard Harter) wrote:
    > On Sun, 17 Aug 2008 10:28:23 -0700 (PDT), raashid bhatt
    > <> wrote:


    > >why are GOTO's not used they just a simple JMP instructions what's bad
    > >about them

    >
    > A fundamental problem with the goto contstruct is that it has no
    > API.


    ?? eh?

    > Consider how function calls work. In C a function will
    > have a prototype that specifies the number of arguments, their
    > types, and the type of object that the function returns. A
    > function's prototype is its API.
    >
    > A nice thing about functions is that interaction between the data
    > inside the function and the data in the caller is restricted to
    > the API. That is, functions are decoupled from their callers
    > except through the API.
    >
    > This is not true of the goto construct. Since it has no API, the
    > code in the target of the goto gets its data from the scope
    > containing both the goto and its target.


    most C constructs don't have an "API". Though I am unclear what
    an "API" is. I know API stands for Application Programming
    Interface, but you don't seem to be using it like that.


    > As a result functions with a lot of goto's are like programs with
    > a lot of global variables. Variables can be changed in many ways
    > in unexpected places.



    --
    Nick Keighley
    Nick Keighley, Aug 18, 2008
    #2
    1. Advertising

  3. In article <>,
    Nick Keighley <> wrote:
    >On 17 Aug, 21:13, (Richard Harter) wrote:
    >> On Sun, 17 Aug 2008 10:28:23 -0700 (PDT), raashid bhatt
    >> <> wrote:

    >
    >> >why are GOTO's not used they just a simple JMP instructions what's bad
    >> >about them

    >>
    >> A fundamental problem with the goto contstruct is that it has no
    >> API.

    >
    >?? eh?


    He's using the term in a more general sense than is common.

    He means "like a contract" - i.e., the API is a contract between the
    user and the implemntor - that says "I do this. And you will do this.",
    with an implicit understanding that nothing else will happen. I.e., the
    contract lists everything that will happen, and includes by extension a
    prohibition on anything else happening.

    Well defined functions, that don't use global variables, obey this
    convention. Things like global variables and GOTOs break the convention.
    Kenny McCormack, Aug 18, 2008
    #3
  4. raashid bhatt

    John Bode Guest

    On Aug 17, 12:28 pm, raashid bhatt <> wrote:
    > why are GOTO's not used they just a simple JMP instructions what's bad
    > about them


    It doesn't matter how fast your code is if it can't be understood or
    maintained, and
    undisciplined use of the goto statement can lead to code that is
    difficult understand, debug, and maintain.

    It makes it harder to debug code by inspection. Take the following
    code fragment:

    ...
    i = 0;
    label: i++;
    printf("%d\n", i);
    ...

    What value gets printed for i? Does i ever get initialized to 0?
    Until you account for every instance of "goto label" in the function,
    you don't know. If the author of the code was sane and the functions
    are reasonably small, it's not so bad. As the size of the function
    increases, however, it can get harder to trace. The problem gets a
    *lot* harder with multiple gotos:

    ...
    i = 0;
    label0: i++;
    label1: printf("%d\n", i);
    ...

    Now what gets printed? You have to account for every instance of
    "goto label0" *and* "goto label1".

    I get to repeat the story everyone else here has heard a thousand
    times. A group I was in was tasked with speeding up some code that
    controlled a 3-d graphical display. It was originally written by some
    wirehead who essentially wrote a BASIC program with C syntax. Instead
    of breaking the code out into separate functions, he put *everything*
    in main() (roughly 5000 lines of code) and used about 15 gotos,
    branching both forward and backward, to simulate subroutines.

    It took a co-worker roughly 2 weeks to figure out the actual flow of
    control in the program. Worse, once we figured out how it worked, we
    found we couldn't actually make any changes to speed it up; the code
    was *so* brittle that any change rendered it useless.

    By themselves the goto statements weren't the problem (the BASIC
    programming philosophy and an unfamiliarity with C were the big
    problems), but they made the problem a *lot* worse.

    There *are* times when it does make sense to use a goto, but they are
    few and far between. About the only time I've used them
    professionally was to break out of a deeply nested loop:

    for (i = 0; i < N; i++)
    {
    for (j = 0; j < M; j++)
    {
    for (k = 0; k < L; k++)
    {
    /* do something interesting */
    if (error)
    goto error1;
    }
    }
    }
    ...
    error1: /* handle error */

    Bode's rules for using goto:

    1. Never replace a standard control structure with a goto;
    2. Branch forward only - do not use a goto to branch backward in the
    code;
    3. Never bypass the entry point of a control structure (i.e., never
    branch into a loop or if-then-else block);
    John Bode, Aug 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand Sagar
    Replies:
    2
    Views:
    1,979
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,833
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    2
    Views:
    403
  5. SoftEast
    Replies:
    17
    Views:
    585
    Juha Nieminen
    Apr 1, 2007
Loading...

Share This Page