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.
  • 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
    musefan
    wrote on last edited by
    #13

    Aw man... you can't just drop that on us and not show us the code! That's just mean!!! :((

    S 1 Reply Last reply
    0
    • S Super Lloyd

      avoiding goto by using try/catch/exception for expected behavior! genius! :laugh:

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

      R Offline
      R Offline
      raddevus
      wrote on last edited by
      #14

      I was going to chime in that all Exception handling is really goto programming and then I saw DRHuff's excellent reply. People talk about never using goto and then you look at their code and see exception handling and think, "what's this here?" This is also why exception handling can be a pain in the arse. All that jumping around. Consider what happens if you throw an exception in your catch block -- and it is possible. Things can get really ugly jumping here, bouncing there. :laugh:

      S 1 Reply Last reply
      0
      • Sander RosselS Sander Rossel

        PeejayAdams wrote:

        many years ago

        PeejayAdams wrote:

        and nobody had died

        That's the exact same time my cat died! :wtf: :(( [Dramatic pause] I don't know who you are. I don't know what you want, but I have a very particular lack of skills. I will never be able to find you. But what I do have is two dollars and a Casio wristwatch. You can have one of them.

        Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

        P Offline
        P Offline
        PeejayAdams
        wrote on last edited by
        #15

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

          S Offline
          S Offline
          swampwiz
          wrote on last edited by
          #16

          The only time "goto" is warranted is if in a nested structure where to accomplish the code control, a flag would need to be set and then checked more than once by the control statements within which this decision to break out is needed. I think I have used it 3 times, and all times, I gave a very good comment as to why it was the proper choice.

          L 1 Reply Last reply
          0
          • R raddevus

            I was going to chime in that all Exception handling is really goto programming and then I saw DRHuff's excellent reply. People talk about never using goto and then you look at their code and see exception handling and think, "what's this here?" This is also why exception handling can be a pain in the arse. All that jumping around. Consider what happens if you throw an exception in your catch block -- and it is possible. Things can get really ugly jumping here, bouncing there. :laugh:

            S Offline
            S Offline
            swampwiz
            wrote on last edited by
            #17

            I think the only time that exception handling should be used is when the current code section does not have any control over the code that is throwing the exception - i.e., it is preferable to check and control something than to rely on an exception that simply does a goto the catch branch.

            R R 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!

              G Offline
              G Offline
              glennPattonWork3
              wrote on last edited by
              #18

              it's an old view but as a Hardware/Firmware type I cannot see the hatred for goto, I mean the jmp is a direct map and it produces a smaller binary. Goto is like an axe very useful but very dangerous. An axe wont cut your hand off, misuse it and you could be missing a hand very easily!

              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!

                R Offline
                R Offline
                RJOberg
                wrote on last edited by
                #19

                OriginalGriff wrote:

                teachers who introduce it early should be hung, drawn, and quartered (and I don't mean "well hung", "sketched", and "given an apartment").

                My CS teacher introduced them early. However it was more of a "Here is the goto statement, here is how it can go horribly wrong, and that is why you should never use it again... ever. If you do, you will fail whatever assignment it was in." sort of way. While it was a bit over dramatic, she did follow up with how to better handle branching/looping logic without them.

                1 Reply Last reply
                0
                • S swampwiz

                  The only time "goto" is warranted is if in a nested structure where to accomplish the code control, a flag would need to be set and then checked more than once by the control statements within which this decision to break out is needed. I think I have used it 3 times, and all times, I gave a very good comment as to why it was the proper choice.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #20

                  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 1 Reply Last reply
                  0
                  • M musefan

                    Aw man... you can't just drop that on us and not show us the code! That's just mean!!! :((

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

                    Mm.. it took me 2 hours to write the function.. that doesn't have goto in it in the end! :laugh: To be fair both the function was complicated and I was distracted! ;P

                    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!

                      G Offline
                      G Offline
                      gardnerp
                      wrote on last edited by
                      #22

                      Obligatory XKCD[^] GOTO is perfectly fine in some scenarios. It's a language feature not a bug. I have used maybe 4 over the past 15 years. So not common, but absolutely has its place.

                      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
                        Munchies_Matt
                        wrote on last edited by
                        #23

                        Why not use them? Sometimes they make sense. Spaghetti code is spaghetti code regardless whether it uses classes, is structured, or just plain crap.

                        G 1 Reply Last reply
                        0
                        • M Munchies_Matt

                          Why not use them? Sometimes they make sense. Spaghetti code is spaghetti code regardless whether it uses classes, is structured, or just plain crap.

                          G Offline
                          G Offline
                          glennPattonWork3
                          wrote on last edited by
                          #24

                          Coming from a embedded back ground, GOTO maps directly to JMP! write assembly without JMP. Like you said I battled with object oriented code that is c*ap but well structured. I like anything don't blame the tools, blame the tool using the tool! ;)

                          M 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
                            Forogar
                            wrote on last edited by
                            #25

                            I once used a "computed goto" - it was in FORTRAN where this kind of thing used to be expected! X| GOTO is my go-to command I go to when I need a goto.

                            - I would love to change the world, but they won’t give me the source code.

                            1 Reply Last reply
                            0
                            • G glennPattonWork3

                              Coming from a embedded back ground, GOTO maps directly to JMP! write assembly without JMP. Like you said I battled with object oriented code that is c*ap but well structured. I like anything don't blame the tools, blame the tool using the tool! ;)

                              M Offline
                              M Offline
                              Munchies_Matt
                              wrote on last edited by
                              #26

                              I have seen some terrible C++ code, the worst was a child of a parent, instantiated as the parent. SO child * myclass = new parent. WTF! Which frigging function is going to get called, the base class or the overridden one! I wanted to punch the person who wrote that.

                              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!

                                C Offline
                                C Offline
                                Chris Losinger
                                wrote on last edited by
                                #27

                                Find In Files / Current Project / "goto" Matching lines: 1254 it's common in C, as it's the cleanest way to handle error conditions.

                                1 Reply Last reply
                                0
                                • S swampwiz

                                  I think the only time that exception handling should be used is when the current code section does not have any control over the code that is throwing the exception - i.e., it is preferable to check and control something than to rely on an exception that simply does a goto the catch branch.

                                  R Offline
                                  R Offline
                                  Rick York
                                  wrote on last edited by
                                  #28

                                  I can agree with that. I had the misfortune of having to deal with an automation systems framework that was essentially a state machine library and it changed states by throwing an exception. I thought it was incredibly stupid. This framework was spreading throughout one division of a customer until someone finally pulled their head out and asked what is this pile you have subjected us to? This particular customer joined my very short list of the worst ones I have ever dealt with and I refuse to buy any of their products to this day.

                                  "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                                  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!

                                    N Offline
                                    N Offline
                                    Nelek
                                    wrote on last edited by
                                    #29

                                    In high level languages... Ok, maybe you have more possibilities and you should avoid using goto's But try to programm in Assembly, in PLC LAD or other similar "low" level languages without it. It is like many other things. Use it wisely and there would be no problems. Abuse or misuse it... and you will burn in hell (or you will be damned and sent to hell by the people who inherit your code)

                                    M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                    1 Reply Last reply
                                    0
                                    • D DRHuff

                                      To avoid this you can put the entire code block in a try. Put the catch at the go to label and throw an exception where you call the goto. Because that’s different somehow!

                                      Socialism is the Axe Body Spray of political ideologies: It never does what it claims to do, but people too young to know better keep buying it anyway. (Glenn Reynolds)

                                      N Offline
                                      N Offline
                                      Nelek
                                      wrote on last edited by
                                      #30

                                      And what happens when the language you are programing in offers no try-catch-throw?

                                      M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                                      1 Reply Last reply
                                      0
                                      • S swampwiz

                                        I think the only time that exception handling should be used is when the current code section does not have any control over the code that is throwing the exception - i.e., it is preferable to check and control something than to rely on an exception that simply does a goto the catch branch.

                                        R Offline
                                        R Offline
                                        raddevus
                                        wrote on last edited by
                                        #31

                                        agreed. I'm thinking of calls to another library where it throws a specific exception after being configured properly. In that case all inputs are set to valid values but then something happens which causes the exception - network not available or something.

                                        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!

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #32

                                          while ( true ) { ... break ... }

                                          C 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