Like Tree19Likes

Is GOTO a bad idea ever?

This is a discussion on Is GOTO a bad idea ever? within the C Programming forums, part of the General Programming Boards category; Hi all! Well programmers are unanimous when it comes to use the goto keyword: everybody aggrees it's a bad idea ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brasília, Brazil
    Posts
    10

    Is GOTO a bad idea ever?

    Hi all!

    Well programmers are unanimous when it comes to use the goto keyword: everybody aggrees it's a bad idea to use it AT ALL.

    Is that really true? For example, this code:

    Code:
    int main()
    {
        for( ; ; )
        {
            RoutineA();
            RoutineB();
            RoutineC();
        }
    }
    Works well on most compilers, but I find the

    Code:
    int main()
    {
        MyRoutines:
            RoutineA();
            RoutineB();
            RoutineC();
        goto MyRoutines;
    }
    a more cross-platform aproach (when compiling the same code in different compilers, for example. Note that the for statement, when used without parameters, is not accepted in every compiler).

    Well is goto a REALLY bad idea as many people say?
    Is there any author in related literature who mentions this subject satisfactory?
    What are the cases I could precisely use it?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The main uses of goto in C are for multi-level breaks (getting rid of a ridiculous uses of flags) and error handling.

    Can you name a compiler that doesn't accept for( ; ; ) ? And even so, what's wrong with while(1) ?

    And presumably your title should have been "is goto a bad idea always"
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by artur View Post
    Is there any author in related literature who mentions this subject satisfactory?
    Search for "Goto considered harmful".
    There is a historical paper by Djikstra in that name.

    What are the cases I could precisely use it?
    Afaik, it is sometimes used for error handling and avoiding flaggy code.
    I myself have no problem with flaggy code, however.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    I've heard some people say that there are some legitimate uses for goto, but personally I've never been presented a convincing case.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by manasij7479 View Post
    Afaik, it is sometimes used for error handling and avoiding flaggy code.
    I myself have no problem with flaggy code, however.
    "flaggy" code (I might remove the ell there) is both inefficient and less readable. It is inefficient in the obvious way: another variable on the stack and operations on that variable. It is less readable because it adds another variable for the reader to keep track of. A simple goto to jump out of the inner loop is far far better. Sadly, people have taken the idiotic "goto's are evil" business to heart and have never really thought about it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by antred View Post
    I've heard some people say that there are some legitimate uses for goto, but personally I've never been presented a convincing case.
    You would have to be truly retarded not to see that goto is better in this case:
    Code:
    // with goto
    while (whatever) {
        while (something) {
            if (such_and_such)
                goto break_outer_while;
            actual_work();
        }
    }
    break_outer_while:
    
    // with idiotic flag
    while (whatever && !idiotic_flag) {
        while (something && !idiotic_flag) {
            if (such_and_such)
                idiotic_flag = 1;
            else {
                actual_work();
            }
        }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by oogabooga View Post
    You would have to be truly retarded not to see that goto is better in this case:
    Code:
    // with goto
    while (whatever) {
        while (something) {
            if (such_and_such)
                goto break_outer_while;
            actual_work();
        }
    }
    break_outer_while:
    
    // with idiotic flag
    while (whatever && !idiotic_flag) {
        while (something && !idiotic_flag) {
            if (such_and_such)
                idiotic_flag = 1;
            else {
                actual_work();
            }
        }
    }
    If the outer while needed to clean up something, the goto version would've needed a few "idiotic_label"s !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by oogabooga View Post
    You would have to be truly retarded not to see that goto is better in this case:
    Code:
    // with goto
    while (whatever) {
        while (something) {
            if (such_and_such)
                goto break_outer_while;
            actual_work();
        }
    }
    break_outer_while:
    
    // with idiotic flag
    while (whatever && !idiotic_flag) {
        while (something && !idiotic_flag) {
            if (such_and_such)
                idiotic_flag = 1;
            else {
                actual_work();
            }
        }
    }
    Code:
    void func()
    {
        while (whatever) {
            while (something) {
                if (such_and_such)
                    return; // <-- no goto needed
                actual_work();
            }
        }
    }
    Elkvis likes this.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,456
    a better alternative would be to put the whole nested loop construct in its own function, and when the condition occurs that requires breaking out of both loops, you simply return. very clean, very simple, very clear. in C++, you have the additional option of throwing an exception that gets caught outside the nested loop, but in C, I think the return from function is the best solution for the nested loop.

  11. #11
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Code:
    void func()
    {
        while (whatever) {
            while (something) {
                if (such_and_such)
                    return; // <-- no goto needed
                actual_work();
            }
        }
    }
    But then you have to pass the variables that "whatever", "something", and "such_and_such" depend on to the function. Even assuming this situation could be handled with a void func(void), oogabooga's solution with the goto is clearer to me, because of the intelligent choice of label name.

    If I saw a function call like the one you suggest in someone else's code, I would think "You made me scroll all the way to the bottom to look at this function and then all the way back to where I was, just so you could avoid using a goto that would jump forward maybe three lines. Riiiiiiiight."
    Code:
    while(!asleep) {
       sheep++;
    }

  12. #12
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by TheBigH View Post
    But then you have to pass the variables that "whatever", "something", and "such_and_such" depend on to the function. Even assuming this situation could be handled with a void func(void), oogabooga's solution with the goto is clearer to me, because of the intelligent choice of label name.

    If I saw a function call like the one you suggest in someone else's code, I would think "You made me scroll all the way to the bottom to look at this function and then all the way back to where I was, just so you could avoid using a goto that would jump forward maybe three lines. Riiiiiiiight."
    Moving all that stuff out to a separate function encourages an approach to programming that emphasizes breaking down problems into small, manageable pieces that can be developed, debugged and reasoned about in isolation from the rest of the program. I think that's a very good thing.

    I'm so tired of dealing with functions spanning several screen pages and containing dozens of variables, loops, if-else cascades, etc.
    Elkvis likes this.

  13. #13
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I'm not saying there's no good reasons to spin a block of code out into a separate function. I'm just saying that doing it just to avoid goto is not a good reason because, if that's all you're doing, the function call and return are just gotos with different names.
    Code:
    while(!asleep) {
       sheep++;
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by artur
    a more cross-platform aproach (when compiling the same code in different compilers, for example. Note that the for statement, when used without parameters, is not accepted in every compiler).
    I repeat oogabooga's challenge: name such a compiler. A few years ago, I picked up an old book on C optimisation that recommended using such a for loop over a while (1) because compilers, old when the book was written, tended to optimise the former to that structure whereas they might actually create a loop that tested the loop condition for the latter. There was certainly no caveat that a compiler might choke on this.

    My guess is that if a compiler that does not recognise such a for loop exists today, it would be very specialised for say, embedded programming, hence rendering the notion of a cross-platform approach irrelevant to begin with.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    After reading the whole discussion so far i believe that there are certain cases, like the one oogabooga proposed, in which the use of a goto statement could be considered not harmful in order to make a more simple and readable code. In my particular case i try to avoid the use of a go to statement because i prefer that the logical structure of my program decides wich control flow should be executed by the program, i do this because i can predict the way the program is going to work and my debug of the program is simpler. What i really oppose is the abuse of the go to at least in a high language program.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is goto bad?
    By DarkAlex in forum C++ Programming
    Replies: 28
    Last Post: 12-02-2007, 10:24 AM
  2. GoTo's?
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2007, 03:24 AM
  3. goto
    By chrismiceli in forum C Programming
    Replies: 20
    Last Post: 08-26-2003, 07:19 AM
  4. goto again...
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2003, 07:25 PM
  5. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM

Tags for this Thread


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