"goto", is it a taboo?

This is a discussion on "goto", is it a taboo? within the C Programming forums, part of the General Programming Boards category; Hi, Does existence of "goto" in a code enough to brand it as bad code??? Consider this piece of code: ...

  1. #1
    Unregistered
    Guest

    "goto", is it a taboo?

    Hi,

    Does existence of "goto" in a code enough to brand it as bad code???

    Consider this piece of code:

    short some_function( short arg1 )
    {
    short return_code = 0;
    short ret_code = 0;

    ret_code = function_a( arg1 );
    if ( ret_code != 0 )
    {
    // failed...
    return_code = ret_code;
    goto fexit;
    }

    ret_code = function_b();
    if ( ret_code != 0 )
    {
    // failed...
    return_code = ret_code;
    goto fexit;
    }

    ....
    ....
    ....

    fexit:

    if ( return_code != 0 )
    {
    // release all hogged resources, do necessary cleanup...
    }

    return ( return_code );

    } // end of some_function()


    Would you consider the above code bad?

    The indentation would probably go haywire, but consider it properly indented :-)

    Thanks,
    - Ruchikar.

  2. #2
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    Yes goto can be used (It is included by ANSI committee in the first place). But it is recommended ONLY when there is no other way to control program flow in C (but there are always other ways to control the flow other than goto). But it does not mean that using goto is always bad.

    Al Kelly & Ira Pohl (authors of C by Dissection) advices that its use , if ever, be always forward on to the program and not backwards. Otherwise, you may get bugs that are quite hard to trace

    But I don't use goto. It undermines the capabilities of block and structured programming. It meddles up functions and produces lots of spaghetti code too. (Yum!)
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > But it does not mean that using goto is always bad.
    Sure it does.
    It displays a close-minded, scape-goat mentality.
    The world is waiting. I must leave you now.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > The indentation would probably go haywire, but consider it properly indented :-)
    The miracle of code tags.
    The world is waiting. I must leave you now.

  5. #5
    Unregistered
    Guest
    close minded, scape-goat mentality?

    The intention in the above code is to keep a single point of exit from the function (centralized cleanup logic).

    Do you guys think the code I listed in my first post is:
    1) an example of spaghetti code
    2) made complex because of "goto"


    Thanks,

    - Ruchikar.

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    The reason I said that is because goto is VERY easy to use, yet very poor form. It's hard to read. If it's a very small example, which is a rare occurance in the code, then no, it's not all that bad. But, if your code relies on goto statements, then I think my comments were appropriate. After all, you're taking the easy way out when there is a better, and more robust way.

    1) an example of spaghetti code
    2) made complex because of "goto"
    If your code is minute, then no.
    If your code is very long, and you EVEN only have one small goto statement, then yes.
    It's hard to follow code when it has a goto statement.
    Simply because, I should be able to goto a certain spot in your code, and see a function call. I simple open the source file that has that function in it, review it, then return to the spot in the code for further reading.

    If you have alot of goto statements, or even one in a large source, then I am flipping all over the place through your code.
    The world is waiting. I must leave you now.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Would you consider the above code bad?

    Yes, if you understood the logic of your function, then you would see that there is no need for goto. If you have ever taken a look at large and complex software, you would understand why those programmers didn't use goto.

    In my opinion, there is never need for goto. And if a programmer says that in his/her code it is really needed, than the logic of that code is badly structured.

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > And if a programmer says that in his/her code it is really needed,
    ....then I don't want them behind my products!
    The world is waiting. I must leave you now.

  9. #9
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    The use of goto may be appropiate when breaking out of nested loops:

    Code:
    while (exp1) {
      while (exp2) {
        while (exp3 {
          while (exp4) {
            if (reason to get out)
              goto quit;
          }
        }
      }
    }
    
    quit: ;

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The use of goto may be appropiate when breaking out of nested loops:
    Not really. When the program gets to the quit: label, it has no idea at which point it jumped from. It therefore difficult to cater for every eventuality with this style. It's also difficult to debug when things go wrong.

    Besides, if you have too many nested loops, you'd probably want rethink your code structure anyway.... but that's a topic for another thread!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >The use of goto may be appropiate when breaking out of
    >nested loops:

    I agree with Hammer. There are plenty of ways to avoid such constructions as you, char, showed.

  12. #12
    Unregistered
    Guest
    Thanks for all the inputs.

    I myself am no fanatic-for-goto.

    Here's the complete story:

    <story>

    I was thinking about the code-structure of a function (let's say primary function). This primary function calls four other functions (secondary functions). These secondary functions return 0 if they are successful. Otherwise they return an integer indicating failure condition.

    The primary function needs to call the secondary functions sequentially and if any of them returns unsucessful, it should not call the rest. In such a case, primary function also returns unsucessfull, it basically propagates the unsuccessful condition code returned by the secondary.

    The primary function also does some malloc()s in between the calls to secondaries, which need to be free()d if the primary function is returning an unsuccessful condition.

    </story>

    The code I had posted in my initial post, was how I thought it can be done most simply and without compromising the readability. But it was using goto statements (only for FunctionExit, where I was doing the clean-up, if needed).

    Can you guys show me a code structure which would do the above more cleanly. I'd appreciate any help.


    Thanks,

    - Ruchikar.

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Well as far as your primary / secondary function "food chain" with your exit function, here's a basic example:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void wait( int seconds );
    int function1( void );
    int function2( void );
    int callexit( void );
    
    int main( void )
    {
            printf("Calling function 1 in 3 seconds...\n");
            wait(3);
            if( function1() == 0 )
            {
                    printf("Function 1 executed properly\n");
            }
            else
            {
                    printf("Function 1 errored!\nClosing...\n");
                    wait(3);
                    callexit();
            }
            printf("Calling function 2 in 3 secconds...\n");
            wait(3);
            if( function2() == 1 )
            {
                    printf("Function errored!\nClosing...\n");
                    wait(3);
                    callexit();
            }
            else
            {
                    printf("Function 2 executed properly\n");
            }
            return 0;
    }
    
    int function1( void )
    {
            return 0;
    }
    
    int function2( void )
    {
            return 1;
    }
    
    int callexit( void )
    {
            printf("Exit function was called\n");
            return 0;
    }
    
    void wait( int seconds ) 
    {
        clock_t endtime = clock() + seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    I put the time stuff in there so the program wouldn't fly by.
    Remove it if you want.
    Last edited by Shadow; 05-28-2002 at 12:37 AM.
    The world is waiting. I must leave you now.

  14. #14
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb Hmmm...

    For once Shadow and I agree on one thing: goto is too easy to use but very bad for super duper hyper ultra long codes like 500,000 lines because they reflect a closed-mind and unorganize programming practice.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  15. #15
    *
    Guest
    I very _rarely_ have used goto-- primarily it is used in performance code, when you don't want the compiler generating a 'test' branching condition.

    The purist fool who said 'goto' should _never_ be used

    a) wasn't a real, professional developer
    b) didn't understand the basics of the computer
    c) was clueless about the language, and
    d) never made a distinction between the compiler and the processor.

    Judicious use of goto is certainly approved.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "goto" question
    By Kylecito in forum C++ Programming
    Replies: 29
    Last Post: 02-15-2006, 05:46 AM
  2. I know everyone hates "GOTO" but...
    By anderson in forum C++ Programming
    Replies: 15
    Last Post: 02-13-2006, 10:08 AM
  3. when to use a "goto"
    By mitiwi in forum C Programming
    Replies: 1
    Last Post: 03-08-2005, 10:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21