Thread: Why is goto bad?

  1. #16
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Yeah... I need to remake my programs.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The variable choice is initialized at the top of the function. Thanks for the help.
    You mean you changed the code to initialize it now? If you meant that in the code you posted it is already being initialized, then that's not correct. It is defined but not given an initial value. If you mean you changed the code to initialize the value, then never mind this post.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brewbuck View Post
    There are no programming problems that can be solved only by goto.
    There is definitely a good reason to use goto, but it's also more deprecated in C++. THE best way of using it is to jump to the end of the function to take care of cleanups, especially if that function has a lot of exit points.
    In C++, this can also be done with try/catch which might be a better solution, though.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> THE best way of using it is to jump to the end of the function to take care of cleanups, especially if that function has a lot of exit points.

    In C++ that should be done with RAII. It should not be done with try/catch or goto.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Classes to destruct things? All things won't work that way. In Windows apps, there's a lot to do at the end of a function presumably (set window texts, set progress bar progress, manage any threads, etc, etc), so goto or try/catch would be a better choice.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Haven't we had this discussion before?

    >> Classes to destruct things?
    Yes, that's basically what RAII is. What do you think a smart pointer is? It is a class designed to clean up the pointer when it is appropriate.

    >> All things won't work that way.
    I disagree. Anything you can do at the end of a function you can do in a destructor. If you have an example of something that can't I'd be willing to listen.

    >> (set window texts, set progress bar progress, manage any threads, etc, etc)
    All of these should be implemented with RAII or some other mechanism (like an extra function call). A goto or try/catch should not be used for this in C++.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Whatever. You win.

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    There is definitely a good reason to use goto, but it's also more deprecated in C++. THE best way of using it is to jump to the end of the function to take care of cleanups, especially if that function has a lot of exit points.
    In C++, this can also be done with try/catch which might be a better solution, though.
    goto is not deprecated in C++ ("deprecated" has a specific meaning in the C++ standard, meaning it is identified for removal from a future version of the standard, so the description "more deprecated" has no meaning).

    It is true that C++ provides more alternatives to using the humble goto statement than does C. Although exception handling is actually more comparable to using a setjmp()/longjmp() combination than it is to using a goto.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When using deprecated, I wasn't implying that it's deprecated or going to be removed, but that it should be avoided because better solutions exist, mostly.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think "bad practice" would be a better term.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, recently someone posted code that was structured like this:
    Code:
    loop:
        do something
        if some condition
    end:
           do something
           stop program
        if loop again
           goto loop
       goto end
    As you can see, the structure is seriously messed up: the end part of the program is in the middle of main().

    Beginners probably find goto easier to use out of inexperience: being not used to express their ideas in higher-level flow control structures.

    Another typical misuse might look like this:
    Code:
        if A
            goto doA
        if B
            goto doB
        goto skip
    doA:
        do something
        goto skip
    doB:
        do something
    skip:
        program resumes here
    Reason? The programmer has mastered checking conditions and goto and that's about that. This would be much better with if else blocks, or switch and/or using functions when "do something" is long.

    All programmers can understand higher level control flow structures easily ("That's a for loop, the initial condition is x, end condition is y, increment is z"), but each structure built with goto's is unique and requires a lot of effort to figure out. Given sufficiently large code size (someone misusing goto would probably also have an enormous main function), non-trivial problem, poorly named variables etc and it might be virtually impossible to figure out how the code works and how it is supposed to be modified if you want to change something.
    Last edited by anon; 12-02-2007 at 09:18 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #27
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    So would this be good for replacing goto in my code?
    Code:
    int main()
    {
        bool invalid = false;
        int choice = 0;
        cout << "Game Name Here v 1.0\n";
        while (choice != 4)
        {
              cout << "Main Menu:\n";
              cout << "1. Play Game\n";
              cout << "2. Instructions\n";
              cout << "3. Credits\n";
              cout << "4. Quit\n";
              do
              {
                   
                   cin >> choice;
                   switch (choice)
                   {
                          case 1:
                               play();
                               cout << "Game Name Here v 1.0\n";
                               invalid = false;
                               break;
                          case 2:
                               instruct();
                               cout << "Game Name Here v 1.0\n";
                               invalid = false;
                               break;
                          case 3:
                               credit();
                               cout << "Game Name Here v 1.0\n";
                               invalid = false;
                               break;
                          case 4:
                               invalid = false;
                               break;
                          default:
                                  cout << "\a";
                                  invalid = true;
                                  break;
                   }
              } while (invalid==true);
         }
        return 0;
    }

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yep, that works. I might set invalid to false before the switch so that you don't have to do it in every case, but otherwise that's just fine. (I'd also check the return value of cin for more error checking as I mentioned earlier, but that's unrelated to this problem.)

    It may not seem that much different, but the fact that there is a possible inner loop is now much clearer. Programmers are tuned to look for for, while and do while, but a goto label has any name and doesn't necessarily standout amongst the rest of the code.

  14. #29
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Thanks. Now I have to go back and fix my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. goto statement interpretation in VC 6 and 7
    By w262 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 10:37 PM
  2. Not important, only a curiosity...
    By BrownB in forum C Programming
    Replies: 35
    Last Post: 12-27-2004, 03:32 PM
  3. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM