Thread: Why is goto so bad?

  1. #1

    Question Why is goto so bad?

    I have asked this on another board but the only answer I got was it hurts readability. Why is goto so bad to use? Take a code like this for example-

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
        begin:
    	float first, second;
    	char option, repeat;
    	cout << "\nFirst number: ";
    	cin >> first;
    	cout << "\nSecond number: ";
    	cin >> second;
        menu:	
    	cout << "\n\n1. Add" << endl;
    	cout << "2. Subtract" << endl;
    	cout << "3. Multiply" << endl; 
    	cout << "4. Divide" << endl;
    	cout << "Option (1/2/3/4): ";
    	cin >> option;
    	if (option=='1')
    		cout << "\nAnswer: " << first + second << endl;
    	else if (option=='2')
    		cout << "\nAnswer: " << first - second << endl;
    	else if (option=='3')
    		cout << "\nAnswer: " << first * second << endl;
    	else if (option=='4')
    		cout << "\nAnswer: " << first / second << endl;
    	else
    	{
    		cout << "That is an invalid option. Please select a valid one." << endl;
    		goto menu;
    	}
    	cout << "\nAgain? (y/n): ";
    	cin >> repeat;
    	if (repeat=='y')
    		goto begin;
    	else
    		exit(0);
    	return 0;
    }
    What is so bad about using those goto statements there? I dont think it hurts readability at all. Others say use loops, that they are more clearer and alot easier. But it seems to me that goto statements are easier than loops, and seem to be just as efficent.

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ......

    Goto promotes spaghetti code, something to leave to assembly with it's jmp statements. You use high-level code to promote functionality and ease of maintenence and managing a bunch of labels when it's completely unnecessary is not good code practice.
    Last edited by rmullen3; 01-01-2003 at 10:27 PM.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    17
    I'm a mediocre programmer, but I'd like a stab at this one myself. Correct me if I'm wrong guys, but this just seems more readable.



    code:--------------------------------------------------------------------------------
    #include <iostream>

    using namespace std;
    int main()
    {
    float first, second;
    char option, repeat = 'y';
    cout << "\nFirst number: ";
    cin >> first;
    cout << "\nSecond number: ";
    cin >> second;

    while(repeat == 'y')
    {
    cout << "\n\n1. Add" << endl;
    cout << "2. Subtract" << endl;
    cout << "3. Multiply" << endl;
    cout << "4. Divide" << endl;
    cout << "Option (1/2/3/4): ";
    cin >> option;

    switch(option)
    {

    case 1:
    {
    cout << "\nAnswer: " << first + second << endl;
    break;
    }
    case 2:
    {
    cout << "\nAnswer: " << first - second << endl;
    break;
    }
    case 3:
    {
    cout << "\nAnswer: " << first * second << endl;
    break;
    }
    case 4:
    {
    cout << "\nAnswer: " << first / second << endl;
    break;
    }
    default:
    {
    cout << "That is an invalid option. Please select a valid one." << endl;
    break;
    }
    cout << "\nAgain? (y/n): ";
    cin >> repeat;
    }


    return 0;
    }
    --------------------------------------------------------------------------------

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    It would have been if you had used code tags.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Why is goto so bad?

    Gotos are unnecessary most of the time. The example you gave trying to defend goto was really weak. Those two labeled sections ought to be functions. Here's a much better version of what you posted:
    Code:
    int main()
    {    
    	char option, repeat;
    	do
    	{
    		float first, second;
    		GetData(first, second);
       		Menu(first, second);
    		cout << "\nAgain? (y/n): ";
    		cin >> repeat;
    	}while(repeat=='y')
    
    	return 0;
    }
    There, that's what we mean by easier to read. This version of your program also promotes information hiding. In addition, because GetData and Menu are functions, you can reuse them and call them multiple times. Goto statements destroy the structure that make programs so great.

    Just don't use gotos, man. They're bad. Also, they are being faded out by the programming community. Pretty soon, you may not be able to use gotos at all. Then, you'd be screwed. You should also learn to not use gotos so that you learn to use functions more. Modularity is very good. If you become comfortable using functions, you'll have an easier time with OOP. Just don't use gotos.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Ok, I see your point. And yes that was a very weak example but hey, I am still new. I still have plenty of time to learn.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Munkey01
    Ok, I see your point. And yes that was a very weak example but hey, I am still new. I still have plenty of time to learn.
    It's nice to see someone with humility around here. Yeah, you're going to learn tons. There's a lot of stuff in C++ that you can get away with, but that you shouldn't do.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    I think you shouldn't use gotos, but should be familiar with label jumping. If you ever get into assembly you have to use that stuff...

    For some reason, ASM's jmp command seems less dirty than goto ... ^_^

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: ~

    Originally posted by rmullen3
    I think you shouldn't use gotos, but should be familiar with label jumping. If you ever get into assembly you have to use that stuff...
    Hmmm, just wondering, doesn't assembly have functions and loops? If not, why not?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Re: ~

    Originally posted by joshdick
    Hmmm, just wondering, doesn't assembly have functions and loops? If not, why not?
    No, rather, yes... but not as you think of them. The instructions in assembler are much less complex than those in C/C++. Some single statements in these languages can result in many lines of assembler (like loops, and functions)

    a simple function dosomething(0xBAD, 0xF00D) translates to:

    Code:
    push	0F00Dh
    push	0BADh
    call	@ILT+0(dosomething) (00401005)
    add	esp,8

  11. #11
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    Assembly looks at things broken into more definite pieces. An example of a function was given, and a loop would basically be

    Code:
    LoopLabel:
    mov ecx, 4
    add eax, ecx ; do something
    loopnz LoopLabel
    or using jmp:
    Code:
    LoopLabel:
    mov ecx, 4
    add eax, ecx 
    dec ecx
    cmp ecx, 0
    jne LoopLabel ; jump if not equal
    So the whole methodology of "goto" is used a lot in assembly... just not in HL languages

  12. #12
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by vVv
    ``Goto'' is /not/ bad per se, and it's /not/ good per se -The way you use it defines whether it renders your functions to ``spaghetti code'' or not. Few people seem to get that, it's you who decides if ``goto'' can help you or not.
    True, but newbies don't understand how to use goto properly. I think that when most people tell someone not to use goto, what they're really saying is, 'don't use goto that way'. The way that this person used goto was just really wrong. I think newbies need to be scared into thinking that things like goto are taboo until they learn much more. Then, they'll realize how to use them properly.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  13. #13
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Heh, I didn't look through my own code. Obviously that mov should be before the label.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Talking

    Yeh, very well said vVv...goto is also keyword of c/c++ like everything else if used correctly and sparingly is fine. There are times that I find using goto help a great deal in terms of program robustness and efficiency... I read somewhere that a top notch programmer went about writing a piece of code in honour of "goto" that cannot be written in any other way, do you know that programmer? do you know what the code he wrote?
    At the end of the day, it's a mattter of "horses for courses"
    Personally, I find using while..., do...while, in place of goto, seems to be using more variables and more text to write. goto is neither good or bad, it's just like any other commands to be used in moderation, and there are so many ways to do the same thing. Now I hope I didn't provoke any rage and annoyances.
    ---------------------------------------------------------------------------------
    HUMBLE YOURSELF AND YOU SHALL BE MADE GREAT....

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I guess Hammer should have closed this thread
    Consider it done. Enough of whats already been said time and time before, and enough of the asm quotes!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/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