Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I just used a goto!

I just used a goto!

Scheduled Pinned Locked Moved The Lounge
csharpcom
66 Posts 45 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Delphi 7 Solutions

    These days the "goto" is back, I find this very unfortunate. Every time someone writes a RETURN or a BREAK statement, he/she is actually writing a GOTO And the worse thing about it is that this is encouraged these days...

    S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #44

    he's back, and he's angry! ;P however I do believe that while do jump the code pointer, they are their own assembly instruction! ;P

    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

    1 Reply Last reply
    0
    • L Lost User

      goto is NOT the enemy, it's only when it's used wrongly. but also ... contrary to popular belief disguising it fixes nothing ... if you really need to GOTO, just do it - hiding it only increases the bullshit level. unfortunatelly, there are teachers that tell the kids "GOTO is bad," but then "to do the same thing..." they then go on and teach the kids how to disguise them, i.e. instead use THROW, or stick the [inner] code in a method and return early (i.e. still in an indefinite state). ffs, really! I've got no problems with goto, but I do have issues with (1) shit code, and even more (2) attempts to hide shit code

      Message Signature (Click to edit ->)

      M Offline
      M Offline
      Mike Winiberg
      wrote on last edited by
      #45

      Well said! Not using GOTO has become a matter of faith rather than good programming practice. I've seen some totally impenetrable code that has a indeterminate state, all because of the convoluted efforts the devs went through to avoid a GOTO. (Including rafts of local variables called things like a, aa, aaa, aaaa, x, xx, xxx, xxxx etc!) GOTO is just another tool - it can be used appropriately or badly, just like any other...

      1 Reply Last reply
      0
      • P PeejayAdams

        I'm sure it was the week after your cat died but I'll have the Casio just in case!

        Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain

        F Offline
        F Offline
        Fueled By Decaff
        wrote on last edited by
        #46

        Phew, just in time.

        1 Reply Last reply
        0
        • D Daniel Pfeffer

          Slacker007 wrote:

          I have to GOTO the store

          Except that's really a function call - you do intend to RETURN from the grocery store, don't you?

          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

          F Offline
          F Offline
          Fueled By Decaff
          wrote on last edited by
          #47

          Ah, a GOSUB

          1 Reply Last reply
          0
          • S Super Lloyd

            I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            F Offline
            F Offline
            Fueled By Decaff
            wrote on last edited by
            #48

            The GOTO statement used to be pure evil. In BASIC it used to go to a line number. Not very easy to understand what it is intended to be doing at the best of times. Add in the fact that you could renumber every line in the program with the RENUMBER command and the GOTO statements would not update to reflect the line number changes and you have GOTO hell. (Not to mention that a lot of versions of BASIC at the time had missing features, for example procedures, functions, while loops, repeat loops.) These days with the GOTO statement going to a label it has had it's teeth filed and, while not a common feature is nothing to be afraid of.

            1 Reply Last reply
            0
            • S Super Lloyd

              I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              F Offline
              F Offline
              Fabio Franco
              wrote on last edited by
              #49

              What language?

              To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

              S 1 Reply Last reply
              0
              • M Marc Clifton

                1. Create a workflow manager using generics, interfaces, class abstraction, enums for state and continuation. 2. Break up your code into small workflow steps. 3. On each step, check for termination. 4. Or to get really fancy, return a code that says what step in the workflow to skip to. 5. If you really want to be cool, allow for async workflow steps! ;)

                Latest Article - A 4-Stack rPI Cluster with WiFi-Ethernet Bridging Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #50

                When can I read the article ?

                «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                1 Reply Last reply
                0
                • S Super Lloyd

                  I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  T Offline
                  T Offline
                  TNCaver
                  wrote on last edited by
                  #51

                  See my signature.

                  If you think 'goto' is evil, try writing an Assembly program without JMP.

                  1 Reply Last reply
                  0
                  • F Fabio Franco

                    What language?

                    To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #52

                    C#

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    F K 2 Replies Last reply
                    0
                    • S Super Lloyd

                      I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                      S Offline
                      S Offline
                      sparklesalt
                      wrote on last edited by
                      #53

                      Use of GOTO is not always bad, nor a sign of poor design. Have a look at the Linux kernel source code sometime. You will see it littered with GOTOs everywhere. GOTO is an elemental part of their error-handling paradigm, they use it in a defined and regular way. The Linux kernel is not the only major and highly regarded software project to use this method, either, it just may be the most prominent. But that said, a colleague of mine tried to employ the same paradigm to some of our own in-house code, and upper management pitched a hellacious fit when they got wind of somebody trying use GOTOs. A group of developers (very good ones) mounted a defense of it, but it was no use. Everybody flatly over-ruled, everybody shamed for having the thought.

                      1 Reply Last reply
                      0
                      • S Super Lloyd

                        I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                        G Offline
                        G Offline
                        Gerardo Orozco
                        wrote on last edited by
                        #54

                        IMHO goto's can really offer a very clean solution when coding in C and you want to implement a sort of manual "finally" or "rollback" logic. Here is an example of code that is rolling back some state if something goes wrong. I like it because the intent is clear (there is a single label we are jumping to), I'm avoiding repeating cleanup code and I'm not introducing additional indentation levels that would be necessary with a structured approach - if we want to avoid multiple exit points.

                        static STATUS Android_PrepareAudioBuffers(void) {
                        STATUS errCode;
                        SLresult result;
                        _slBufferQueueItf = NULL;
                        /* Obtain interface to the AudioBufferQueue object */
                        result = (*_slRecorderInstance)->GetInterface(_slRecorderInstance, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
                        &_slBufferQueueItf);

                        if( result != SL_RESULT_SUCCESS ) {
                        LOGE("%s - Error calling _slRecorderInstance->GetInterface(SL_IID_ANDROIDSIMPLEBUFFERQUEUE)", __func__);
                        errCode = S_ERR_OPENSLES_GET_INTERFACE;
                        goto fail_point;
                        }

                        /* Register buffer-ready call-back */
                        result = (*_slBufferQueueItf)->RegisterCallback(_slBufferQueueItf, Android_RecordCallback, NULL);
                        if( result != SL_RESULT_SUCCESS ) {
                        LOGE("%s - Error calling _slBufferQueueItf->RegisterCallback()", __func__);
                        errCode = S_ERR_OPENSLES_REGISTER_CALLBACK;
                        goto fail_point;
                        }

                        /* Specify an event mask to ensure the callback fires only when an audio buffer is full */
                        (*_slRecorderItf)->SetCallbackEventsMask(_slRecorderItf, SL_RECORDEVENT_BUFFER_FULL);

                        _activeBuffIdx = 0;
                        /* Initialize buffer queue by enqueing all buffers at once.
                        When a buffer is filled, it is dequeued and the registered call-back is called
                        The buffer should be re-enqueued when processed to implement circular-buffer-list
                        functionality */

                        int i;
                        for( i = 0; i < RECORD_BUFFER_COUNT; i++ ) {
                        result = (*_slBufferQueueItf)->Enqueue(_slBufferQueueItf, _buffers[i], _buffSz);
                        if( result != SL_RESULT_SUCCESS ) {
                        LOGE("%s - Error calling_slBufferQueueItf->Enqueue()", __func__);
                        errCode = S_ERR_OPENSLES_ENQUEUE_BUFF;
                        goto fail_point;
                        }
                        }
                        return S_OK;
                        fail_point:
                        // rollback state
                        if( _slBufferQueueItf ) {
                        (*_slBufferQueueItf)->RegisterCallback(_slBufferQueueItf, NULL, NULL);
                        (*_slBufferQueueItf)->Clear(_slBufferQueueItf);
                        _slBufferQueueItf = NULL;
                        }
                        return errCode;
                        }

                        1 Reply Last reply
                        0
                        • S Super Lloyd

                          I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                          E Offline
                          E Offline
                          englebart
                          wrote on last edited by
                          #55

                          Which language? Probably a member of the "C" family. (C, C++, C#) Java killed goto with the "labeled break" which allows you to only "goto" the statement after a close curly brace.

                          S 1 Reply Last reply
                          0
                          • S Super Lloyd

                            C#

                            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                            F Offline
                            F Offline
                            Fabio Franco
                            wrote on last edited by
                            #56

                            Code sample would be nice. Because honestly, with the amount of control structures and object orientation support it provides I don't see a reason to use it. In fact. I work with C# since I was 24 years old (I am 36 now). Never had to use goto since (except from a few side projects in C). I used moro goto on imperative/structured/procedural languages like C, C++ and BASIC. So on purely objected oriented programming, whenever I thought of goto, it flashed a yellow light to me indicating I probably got some design patterns wrong. After some time I never got that yellow light again. I am not saying your use is not justified, I just can't wrap my head around a reason to use it.

                            To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                            S 1 Reply Last reply
                            0
                            • F Fabio Franco

                              Code sample would be nice. Because honestly, with the amount of control structures and object orientation support it provides I don't see a reason to use it. In fact. I work with C# since I was 24 years old (I am 36 now). Never had to use goto since (except from a few side projects in C). I used moro goto on imperative/structured/procedural languages like C, C++ and BASIC. So on purely objected oriented programming, whenever I thought of goto, it flashed a yellow light to me indicating I probably got some design patterns wrong. After some time I never got that yellow light again. I am not saying your use is not justified, I just can't wrap my head around a reason to use it.

                              To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                              S Offline
                              S Offline
                              Super Lloyd
                              wrote on last edited by
                              #57

                              I didn't use goto in the end.. had to write a public static function and used it instead....

                              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                              1 Reply Last reply
                              0
                              • E englebart

                                Which language? Probably a member of the "C" family. (C, C++, C#) Java killed goto with the "labeled break" which allows you to only "goto" the statement after a close curly brace.

                                S Offline
                                S Offline
                                Super Lloyd
                                wrote on last edited by
                                #58

                                C# used a static public function instead.. because it turned out...

                                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                1 Reply Last reply
                                0
                                • S Super Lloyd

                                  I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                  S Offline
                                  S Offline
                                  sasadler
                                  wrote on last edited by
                                  #59

                                  I grew up on assembly code in the late 70's so I have no issues using goto's in my coding (C/C++ these days) when approriate. The idea that goto's are inherently bad is just bogus. There is a relatively small class of functions that are shorter and easier to understand when coded with a goto as opposed to the convoluted looping required to do the same thing. And being an old fart, shorter and easier to understand is a good thing! Especially when I have to add new features to code I wrote years ago.

                                  1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    The important thing with goto is knowing when not to use it - which is most of the time. :laugh: Goto has its place, but if you can use a structured loop or branch instead then your code is generally cleaner and more maintainable. But very occasionally, using a goto makes your code cleaner or much more efficient and (provided it's well documented) isn't a problem. It's a problem when it's used because they can: and teachers who introduce it early should be hung, drawn, and quartered (and I don't mean "well hung", "sketched", and "given an apartment").

                                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                    K Offline
                                    K Offline
                                    Kirk 10389821
                                    wrote on last edited by
                                    #60

                                    I had a complicated set of "Insurance Premium Calculations" that was all GOTO based because of short-cut logic (If the person was < 25, skip this discount, etc) Ultimately, I used a while (1=1) { // setup initial values // instead of a goto, just a break; // insert 30 different logic checks, each with a break, after setting/adjusting rating parameters. break; } In the end, what's the difference? If there was only 1 label every goto could go to... And there was, then it is pretty much the same. (I was always hopeful nobody put a continue inside that loop!)

                                    1 Reply Last reply
                                    0
                                    • S Super Lloyd

                                      I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                      M Offline
                                      M Offline
                                      Michael Breeden
                                      wrote on last edited by
                                      #61

                                      As others have said, it's largely what Try Catch does. It has been pointed out though that using break and return in a method is really the same thing in a way. I've used on goto in my code, but based on that last sentence I plan to use them regularly though in the future, just always within a method. Instead of a catch -> return -> handle error, use a catch -> goto error handling at end of method -> end of method returns.

                                      1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                        G Offline
                                        G Offline
                                        gggustafson
                                        wrote on last edited by
                                        #62

                                        Does anyone remember when the use of gotos was first discussed? Does anyone remember the reason Dijkstra gave for their elimination? To me, I'm sorry, this thread seems somewhat juvenile

                                        Gus Gustafson

                                        1 Reply Last reply
                                        0
                                        • S Super Lloyd

                                          I know "it's bad" and I will be shun by all good developers and rich employers for a thousands years... But I look at it.. I can't find of anything better.. mm, maybe a private inner function (i.e. function inside a method).... followed by misplaced return.... mm....

                                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                          M Offline
                                          M Offline
                                          Member 9167057
                                          wrote on last edited by
                                          #63

                                          It's not bad, it's dangerous. But there's use cases where it's better than, let's say, 12 nested ifs. Did that myself, some COM-code which calls COM-methods in order I've placed gotos between method calls to directly jump to cleanup/deinitialization/error handling instead of a pile of if NoError=SomeMethod {if NoError=NextMethod). Way cleaner, but requires knowing what I'm doing. One wrong goto and I'm screwed.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups