Like Tree10Likes

I used my first goto statement today!

This is a discussion on I used my first goto statement today! within the General Discussions forums, part of the Community Boards category; Originally Posted by laserlight I would argue that it is not just convention: it is by design. Furthermore, convention plays ...

  1. #31
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    I would argue that it is not just convention: it is by design. Furthermore, convention plays an important role in good design and readability. Granted, sometimes we might ignore the original design intentions and convention, allowing for arcane code in exchange for some benefit (template metaprogramming and Boost.Spirit comes to mind), but here constructs already exist to handle the logic efficiently in a conventional way. There is simply no benefit in abusing exceptions to perform normal flow control. Concerns about compiler bugs aside, your goto example would be a far better solution because that is a well known use for goto (even though it has its detractors allergic to all use of goto).
    Sure, when C++ was conceived, I imagine the only thing Stroustrup had in mind was error propagation. An extension of existing concepts drawn from software and hardware facilities of the time. So what? For me it is a tool that can achieve many interesting and useful things and so I use it as such. A bird's feather, after all, was not "designed" to be a writing instrument and yet it was nonetheless adapted to do so. Nothing on this earth in fact would have found utility had someone not stood up one day and said "I know - I'll use it for this!". Sorry to wax philosophical, but for me the subject is just that.

    Quote Originally Posted by laserlight View Post
    I believe that the common cases where they lead up to a speed up in code execution is because the exception mechanism was not actually invoked. That fits the bill for error handling in that if there is no error, there is minimal cost, as opposed as to always having to explicitly check for the error at the call site. In normal flow control, on the other hand, the condition involved might always become true at some point, as in the case of getting out of a loop.
    In "most" cases, perhaps, but certainly not all. Checking a flag repeatedly within a loop or calling a bunch of "structured" functions may well be less efficient at times.

    Obviously I am playing a bit of the devil's advocate here. For the most part I think my code, by virtue, "tends toward the convention". I just want to make it clear that I do not subscribe to nor promote any sort of "idiomatic usage for the sake of convention" type approach to programming. The mathematical beauty of a well written program transcends and trumps that any day.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #32
    Registered User
    Join Date
    Oct 2006
    Posts
    2,539
    to me, this seems like an appropriate situation for a lambda, if C++11 is available.

    Code:
    for (;;)
    {
        [&]()
        {
            for(;;)
            {
                switch(some_variable)
                {
                    case 23:
                    {
                        return;
                    }
                }
            }
        }();
    }
    I think it's pretty likely that, in this situation, the compiler will optimize the call/return away, and won't bring you any closer to overflowing your stack.
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by Sebastiani
    For me it is a tool that can achieve many interesting and useful things and so I use it as such. A bird's feather, after all, was not "designed" to be a writing instrument and yet it was nonetheless adapted to do so. Nothing on this earth in fact would have found utility had someone not stood up one day and said "I know - I'll use it for this!". Sorry to wax philosophical, but for me the subject is just that.
    Would you also use a while loop where an if statement is appropriate? Would you use an if statement with a label and goto if a while loop was appropriate?

    I have already noted two instances of creative uses of C++ constructs that I find acceptable (template metaprogramming and Boost.Spirit's approximation of EBNF), so it should be obvious that I am in agreement that just because something was not designed to do a job doesn't mean that it cannot be used well for that job. However, when it comes to ordinary flow control, exceptions are not the best tool for the job (and you even gave a like to yet another alternative proposed, though it is along the same lines as what was suggested), hence fitting them into that role is at best creativity for the sake of it rather than to actually arrive at an elegant, efficient and maintainable solution.

    Quote Originally Posted by Sebastiani
    I just want to make it clear that I do not subscribe to nor promote any sort of "idiomatic usage for the sake of convention" type approach to programming.
    Neither do I. I subscribe to idiomatic/conventional usage for the sake of code that will be easily understood. Convention is not a worthy end in itself.

    Quote Originally Posted by Sebastiani
    The mathematical beauty of a well written program transcends and trumps that any day.
    Agreed. I find no mathematical beauty in abusing exceptions for ordinary flow control.
    Last edited by laserlight; 10-16-2013 at 01:16 PM.
    Sebastiani likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,231
    Elkvis, you're God **** right C++11 is available! I've been waiting to find a use for lambda functions ever since Elysia mentioned them awhile ago when I first posted the link to my whole project on the C++ board. Or was it you who suggest a lambda, Elkvis? I think it might've been you. Well, either you or Elysia.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. goto statement help
    By lscamaro in forum C Programming
    Replies: 13
    Last Post: 10-25-2012, 12:47 AM
  2. Trouble using GOTO with the SWITCH statement!
    By xxJaRxx in forum C Programming
    Replies: 9
    Last Post: 03-10-2010, 06:42 AM
  3. GOTO statement in c++
    By kibestar in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2009, 08:10 PM
  4. goto statement interpretation in VC 6 and 7
    By w262 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 10:37 PM
  5. Goto statement
    By _JjC:: in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 10:43 AM

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