Thread: gotos

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    gotos

    I have read many places the gotos are bad, however, the only drawback I can find is that durring a goto code jump, the declaration of a varible may be missed, causing assignments of non-existant varibles. So (other than what I just pointed out) why do you people say "gotos should never be used any where under any curcimstances"?

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I for one do not say that, since it's absurd. I pretty frequently find use for ye olde "error jump", when you have common cleanup code and multiple probable error situations in the same block.

    There are, of course, better ways to handle this (exception handling, RAII) but... I... don't.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> why do you people say "gotos should never be used any where under any curcimstances"?
    Who says that? Please be specific.

    goto's are considered bad because there are often better alternatives that make code more readable and maintainable and less likely to have bugs. This is especially true in C++ which has exception handling and RAII, although both C and C++ have other loop constructs that are better than gotos in many instances.

    In addition, goto's are easier to use and simpler to understand than the better alternatives, so many novice programmers use them as a first resort rather than a last resort. Saying that goto is evil can often impress upon those novice programmers that even if it seems ok in their current circumstance they should strive to find the better alternative.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > >> why do you people say "gotos should never be used any where under any curcimstances"?
    > Who says that? Please be specific.
    The MISRA coding standard for one.

    From a theoretical aspect, you can always construct a program which is free of gotos.

    It's more a case of "dangerous" rather than "bad". If over used, or inappropriately used, what you end up with all to easily is a mess. Jumping backwards for example is a sure sign that you should have used a loop construct.

    There's certainly no need for them in any typical student homework problem.

    But (in C say), a single label denoting the failure exit from a function, which if applied in a consistent way across all functions, can produce something which looks just fine.
    C++ has the edge here, because exceptions do the same thing in a much more structured fashion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like so many things, the saying is "to break the rules, you must first know the rules", and this means that "you shouldn't break the rules by ignorance, but rather because you understand when the rules REALLY matter, and when there is an exception to the rule."

    So, it's fine to use goto's under the right circumstances. The typical example is where you have multiple levels of loops and/or many if-statements before the end of the function, and you can't JUST return with an error-code (e.g. there are locks that need unlocking, files to be closed, etc, etc).

    But using goto's "without knowing why" is a bad thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    As a general principle, if you're going to have gotos, have them jump downwards.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Being primarily a C++ programmer and not C, gotos make me shudder. Exception handling is so much nicer.

    There are exceptions but they are rare. If you show us code where you are using gotos or feel you have to my bet is that almost everyone would be able to show you a way without using goto.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Interesting, I have always just looked at C++ as extended C. Which only makes sence, seeing how my C++ compiles just fine with C code in it.

    Yeah, you are probably right, the only time you'll ever have to use a goto is for preformance. But that doesn't provide a reason not to use it.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I used a goto recently ... made me feel slightly ... dirty cause I didn't need to use it and I was goto-ing backwards too. I just didn't want to loop my program for some reason.
    ... *fixed* ... Makes me feel slightly better I suppose.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I once had some complicated text parsing thing I needed to loop. I ended up using goto after spending a long time on it. Since it worked, I left it as is and used it that way. I later went back and rewrote it properly.

  11. #11
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Bubba View Post
    Being primarily a C++ programmer and not C, gotos make me shudder. Exception handling is so much nicer.
    Oh yes. Never ever ever ever use gotos in C++. In fact, just don't use C++; it's like programming through a minefield.

    (Use D! ;->)
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > I used a goto recently ... made me feel slightly ... dirty
    I'm amused at how using a goto is akin to some sort of confession during the twelve step program in AA. "Oh man I blew it... I bought a beer the other day."

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    "Hello, my name is twomers and I'm a goto man,".

    Yeah. It starts off with the occasional goto but then it just sneaks up on you and goes completely out of control and before you know it you're listening with your eyes, citizn! Your eyes!

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah. It starts off with the occasional goto but then it just sneaks up on you and goes completely out of control and before you know it you're listening with your eyes, citizn! Your eyes!
    Sounds like you need some funrolls. Go ahead, it'll help you see straight going to work tomorrow!

    Hehe, in all seriousness that's probably an okay guideline isn't it?

  15. #15
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Code:
    void draw(){
        if(WeCantSeeTheObject()){
            goto skip;
        }
    
        /* rendering code */
    
    skip: /*do nothing*/ ;
    
    }
    I've found this to be faster than using if/else to skip or run the drawing code for some reason. It was a while ago, so I could've done something else wrong.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GoTo's?
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2007, 03:24 AM
  2. Time limit on cin
    By Queatrix in forum C++ Programming
    Replies: 11
    Last Post: 04-13-2005, 01:56 PM
  3. C++ vs Java
    By Moni in forum C++ Programming
    Replies: 19
    Last Post: 03-23-2003, 04:19 PM
  4. Hehe, look at this code. (Not looking for help)
    By SinAmerica in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2002, 09:30 PM
  5. if, then, and gotos
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2002, 05:56 AM