Does goto have a glitch or...?

This is a discussion on Does goto have a glitch or...? within the C++ Programming forums, part of the General Programming Boards category; Ok, many people dislike goto. As far as I can tell, its just cause it makes the code slightly ugly. ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Does goto have a glitch or...?

    Ok, many people dislike goto. As far as I can tell, its just cause it makes the code slightly ugly. But, I thought about it; I built a loop and a function to make a repeating structure that called itself.

    Then, I remembered the evil goto of doom!

    Code:
     ninty:
     if(x < Ens.size() && Ens.size() > 1) {
      x++;
      ymir = FindEnemy(x);
      if(ymir.IsNull())
       Ens.pop_back(); 
      goto ninty;
     }
    This replace my function, my for loop, everything! But, then I thought, their must be a reason people dont use this... Its pritty hated as far as I can tell... Is their an actual error with the goto statement? Its not a giant loop, its definantly replacable. But its a very nice timesaver, but I dont want to go getting used to error-prone code. (I dont mean logic errors that can be built using goto, I mean built-in errors with the literal goto statement)

    Thanks!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    How is that any different from
    Code:
    while (x < Ens.size() && Ens.size() > 1) {
      x++;
      ymir = FindEnemy(x);
      if(ymir.IsNull())
       Ens.pop_back(); 
    }
    Sure, there are any number of ways you could create a while loop using any number of goto's, but if you really intended it to be a while loop, make it obvious to the reader what you want to happen.

    > Its not a giant loop, its definantly replacable.
    Backward goto's like yours are even worse than forward gotos (emergency nested loop exits for example), since it's basically saying you want a loop of some sort. With for loops, while loops and do loops to choose from, there's really no excuse for inventing a new method of looping.

    > Its pritty hated as far as I can tell
    You've obviously never had to maintain code riddled with gotos before.
    Anything more than exceptional use is abuse IMO.

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Theres an article on it on this very site. As well as too many to count if you searched google.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Higher level constructs are far more readale. You can express your intent directly. The code jumps up and tells you what it does: "repeats these statements while this condition holds," for example, and you don't have to analyze it step by step like. If you work in an environment where other people are likely to see your code or if you are likely to touch the code you write now at some point in the future you will appreciate the extra readability.

    There is a lot to be said about idioms too. Each platform has its idiomatic expressions, like a "standard" way of doing something that everyone uses and recognizes easily. For example the use of a for-loop to iterate over the elements of an array is an idiom that any programmer with experience in C-like languages will recognize. You should use idiomatic expressions as much as possible because people are used to seeing them and therefore they can be debugged more easily.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Though thats all fair game, I did invent a new loop, and my excuse is <NULL>. But that wasnt my question, I'm wondering if theirs an actual error with goto. I actualy think using a goto to break out of a loop is worse - in C++, goto is the only way I know of (besides loops) of going to another point above where the call was made. But, a function can easily break from a nestled loop with a return - if you dont want to break from it all the way, its always possible to segment it.

    But , I'm not looking for a flame war, I'm just wondering if:

    Code:
    if(1+1=2)
     goto end;
    end:
    Has any issues, or if its just the uglyness thats hated. & Joni, I really didint get what you mean <-> did you mean I should structure my code better?

    I'm reading the article now --

    [Edit]
    I read the article, and a few on google. None of them actualy state an error, but most of them are quite prejudice agenst the goto statement. As an unrelated, and probably unholy question, is this alloud?

    Code:
    blah: int yam() {
     cout << "O_O";
     goto skip;
    }
    
    int main() {
     goto blah;
    
     cout << "Skip me!";
     goto notskip;
    
     skip:
    
     cout << "You skipped!";
     goto end;
    
     notskip:
    
     cout << "Boo!";
     goto end;
    
     end:
    }
    Unholy bit of code isint it?
    Last edited by Blackroot; 02-18-2006 at 06:56 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    Of course there's no actual error, it's perfectly valid code which the compiler is perfectly capable of compiling.

    But compilers don't give a rats-ass about how maintainable the code is, or whether any human would want to look at the code and figure out what it does.

    There's plenty of other language abuse as well (just read past IOCCC entries) which are perfectly valid programs which a compiler has no trouble with, but nobody really wants to maintain that code on a serious basis.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    What I mean is that you should write "code that documents itself" -- code that expresses your intent as clearly as possible.

    If your intent is to repeat a bunch of statements while a condition holds, use a while-loop.
    If your intent is to repeat a bunch of statements for values in a certain range, use a for-loop.

    If you find that your intent is most naturally expressed as a bunch of labeled instructions and goto-statements that jump from back and forth, feel free to implement it with "goto." But Perhaps you could find a more understandable way to express yourself. For example, which of these two ways to process all elements of a collection would you understand with least effort?

    Code:
    Using goto:
    1 take an element from the collection
    2 process it
    3 if there are more elements, go to 1
    
    Using for:
    for all elements e of a collection: process e
    There is nothing wrong with the "goto" instruction itself. It's just that there are better alternatives. And that some uses of "goto" have lead to code whose intent is far from clear.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Actually, I'm not positive on this, but I believe there is a problem with the way you used the goto statement inside a function. To me that just calls out all kinds of wrong. If you use goto to break outside a function, which is never returned to, I don't think the pointer to that function is removed off of the program's stack (can cause a stack overflow if called too many times), and any local variables (including parameters) are never freed from memory. That screams unresourceful and will most likely cause more errors than you'd like during runtime.

    I might be mistaken though, not sure if all that is taken care of by compiler or what not, but I can't see why it would be.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    Since goto is in fact illegal between functions, the issue doesn't arise.

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Salem
    Since goto is in fact illegal between functions, the issue doesn't arise.

    I wouldn't have known; I don't use it =p
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 08:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 08:05 PM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 05:21 PM
  5. I need a faster way to do this....
    By frenchfry164 in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2002, 05:05 PM

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