Thread: Why is goto the devil and exiting programs question.

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Keywords such as continue and break do indeed behave like goto; but unlike goto, when I see them, I know exactly where it will jump, because the rules for break and continue are very strict, unlike goto.
    Furthermore, if you would use goto, you need to put a label everywhere. And labels do no follow scope rules, so each label would be unique for every loop in a function. So if I said goto StartOfLoop50, I would first need to know which is the loop 50.
    And then we have to type out more stuff and clutter the code even more, making continue and break easier to read, decipher and type, hence making goto a worse solution. It might just work, I'm not saying it wouldn't, but in these cases, all I say is that it's unnecessary. There are better alternatives.

    In essence I would say: all the legitimate uses of goto has been replaced by other approaches in the language, making goto unnecessary. Where you would use goto to jump around in the code, like the abovementioned example, it's really evil because it hurts readability and maintainability.

    I don't fancy reading through that document. It looks so boring. All I want is some examples (in C++!) where goto really improves some part of the code. I would gladly change my notion of goto. Failing that, I will stick to the tried-and-true notion of that goto is evil.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Goto is valid if it truly simplifies the code.

    If you have a function which takes a couple of locks and then needs to release them [and there isn't a good way using C++ objects to AUTOMATICALLY lock/unlock using the constructor destructor mechanisms], then a goto to a label that unlocks and returns is better than trying to ensure that all return points have the relevant unlocks. I have found more than one bug caused by a return that the programmer forgot to add the relevant unlock(s) (usually a rare error condition [e.g. out of memory], so no one discovered it early on in testing)

    In C++, there is, as I hinted at above, little reason to use locks this way. We can do:
    Code:
    class Lock
    {
    private:
        SomeLockingType &theLock;
    public:
        Lock(SomeLockingType &lock) : theLock(lock)
        {
           theLock.MakeLocked();
        }
        ~Lock()
        {
           theLock.UnLockIt();
        }
    }
    Now there is absolutely no need to manually lock/unlock the lock mechanism.

    --
    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.

  3. #48
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, but there is another way. It's called boost::bind. Use it to bind the unlock functions of all the locks and wrap them in RAII objects whose destructor calls the functor it wraps. I've used this approach before.
    Example:
    Code:
    class ScopedCallerBase
    {
    public:
        virtual ~ScopedCallerBase() { }
    }
    
    template<typename T> class ScopedCaller: public ScopedCallerBase
    {
    public:
        ScopedCaller(T Functor): m_Functor(Functor) { }
        virtual ~ScopedCaller() { m_Functor(); }
    private:
        T m_Functor;
    };
    
    template<typename T> ScopedCaller<T>* CreateScopedCaller(T Functor)
    {
        return new ScopedCaller<T>(Functor);
    }
    
    void MyFunction(Lock& lock1, Lock& lock2, Lock& lock3)
    {
        std::auto_ptr<ScopedCallerBase> AutoRelLock1 = CreateScopedCaller( boost::bind(&Lock::unlock, lock1) );
        std::auto_ptr<ScopedCallerBase> AutoRelLock2 = CreateScopedCaller( boost::bind(&Lock::unlock, lock2) );
        std::auto_ptr<ScopedCallerBase> AutoRelLock3 = CreateScopedCaller( boost::bind(&Lock::unlock, lock3) );
    }
    Somewhat messy, but C++0x will make this easier. I also do believe Boost has some macros and other stuff that simplifies this.
    Of course, ScopedCaller, ScopedCallerBase and CreateScopedCaller are all generic so they'll work with everything.

    Of course, this is a techy C++ solution (a preferred one, I suppose) and easy to read for the trained eye.

    Oh yeah, btw, welcome back, Mats.
    I noticed you haven't been around for a while.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #49
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Elysia View Post
    I don't fancy reading through that document. It looks so boring.
    Well, it really doesn't help your case if you aren't willing to go through a document that is hands-down the most in-depth analysis of the goto statement and find pseudo-code hard to read. I'll be taking that as a "I don't want to know about it. Goto is evil because I think so".
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #50
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, that's fine. After all, it's an opinion by me that you're trying to convince
    The document might be evidence against the goto paradigm.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #51
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Elysia View Post
    The document might be evidence against the goto paradigm.
    It is in fact. Knuth is making a case against Goto statements and for structured programming. But he is doing it in a smart way. He's constructing difficult problems to handle without goto and then deconstructing them, showing how to avoid goto.

    He then proceeds to show us a few cases where goto can make sense, even if it can be easily replaced by some other language feature. Other than the actual examples and his analysis about them, I find this particular quote rather interesting:

    Quote Originally Posted by pag.294 (With go to Statements)
    We've already mentioned that go to's do no have a syntactic structure that the eye can grasp automatically; but in this respect they are no worse off than variables and other identifiers. When these are given a meaningful name[...] we need not apologize for the lack of syntactic structure.[...] In other words, we can consider goto statements as part of a systematic abstraction; all we need is a clearcut notion of exactly what it means to goto each label.
    Note that he concludes with the idea that goto should indeed have been removed from future programming languages as a legislative maneuver, in order to better educate programmers into the benefits of structured programming. Thankfully, such wasn't necessary as you well know. The goto debate today is nothing like what it was back then when structured programming was emerging in the form of such languages as ALGOL or PL/I.

    Let me be clear about what I'm trying to say here...

    That I don't understand the hoopla about goto. It's old and it makes no sense these days since you cannot find anywhere someone actively defending their consistent use in modern languages. It's beating on a dead horse. Yes, we all know goto is not good for most programming. Thank you. Move on. Nothing to see here.

    So, it's instead a lot more meaningful to learn to identify those cases where goto can make a difference. Because all this goto is evil taught us was ignorance of its use. We aren't trained in its use and we don't know how to take advantage of it or even when to identify the need for it. Goto became the tool of expert programmers. Think about it.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #52
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, I do get your point. Now all we actually need is some examples of where goto is a good tool (especially for C++). Then we start digesting good uses of goto and start implementing and experimenting with it elsewhere. Unfortunately, I don't know many good examples, but I am open to good suggestions.
    I hope everyone else is, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #53
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Breaking out of deeply nested loops.

    Java solved it with break/continue with labels. C++ doesn't have that last time I checked.

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's one valid example that I know of. Haven't had much use of it myself, though.
    But are there other situations?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #55
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    whatever... You don't wnt to erad it, don't.

    You refuse to learn how to identify these situations and instead want to see the pretty pictures? Fine, I'll give you examples where goto is today used by expert programmers:

    - Language parsers
    - Complex State Machines
    - Complex traversing algorithms (complex multi-branch tree traversing rings a bell, but i'm shaky on this one because I can't seem to remember the details of what I read a long time ago)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #56
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mario F.
    More important than showing why goto shouldn't be used, is showing when goto should be used. Anyone with a few weeks of C++ will immediately learn to recognize the benefits of a structured language and doesn't need to be told against Goto. If they don't, their careers in software programming will be short and not warrant the debate. But much, much, more difficult is identifying those moments when goto can become useful.
    Keep in mind that this thread was started by Jefff, who is apparently new to (C++) programming. As such, it is more important here to show why goto shouldn't be used than to show when it should be used. As I stated in post #37, "it is only when you are familiar with the rule that you can appreciate when it is time to invoke an exception to it".
    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

Popular pages Recent additions subscribe to a feed