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. goto... Who uses it?

goto... Who uses it?

Scheduled Pinned Locked Moved The Lounge
questionlearning
131 Posts 66 Posters 7 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 DanielSheets

    Joe Woodbury wrote:

    Huh? Oh yeah, I was right:

    You were half right.

    Joe Woodbury wrote:

    Fact is, you don't need the goto and were showed why. Instead of taking that and learning (as I did with the ternary and null) you became defensive. Turns out your code needs to be refactored anyway, so why not fix it right?

    I've not gotten defensive at all? If you think I did, then where? Personally, I like the ternary operator. I like the way it looks and I like the way it works. I'm not a "n00b" programmer by any means. I am, however, completely self taught. I'm also in the process of taking Microsoft certified courses in C# (through the company I work for). Perhaps then I can learn to code to the standards set forth by the majority here. And for the record... Of all of the projects that I've completed here, the code snippet I put here contained the very first use of goto that I've used in years. After thinking about it, its simple and concise. You remind me of a gear snob. In case you're not a musician let me explain that for you. A gear snob is, for example, a guitarist who plays an expensive Fender Strat that looks down on another guitarist that plays a Squier or an Epiphone. You are convinced that goto's and ternary operators are examples of bad programming. So you're condescending to me because I use them. But in the end... I don't care what you think. Your opinion means about as much to me as anybody else's here. And thats exactly what it is. An opinion. How's that for defensive?

    J Offline
    J Offline
    Joe Woodbury
    wrote on last edited by
    #119

    DanielSheets wrote:

    You are convinced that goto's and ternary operators are examples of bad programming.

    Good grief. I didn't say ternary operators were bad; I said that since they are no longer needed in your specific code, they can be removed. What I learned was that the code

    text == "" ? "0" : ...

    does not throw an exception in C# if text is null. My view that gotos are bad is based on years of experience of finding bugs caused by their use (even in assembly where JMPs are a given and cause all sorts of problems when you aren't careful.) Condescension is in the eye of the beholder. In this case, you had a code review, extremely valid suggestions were made on how to improve it. You concede that much of what you are doing isn't needed, but insist on keeping it that way. That's fine, but don't go around blasting everyone for being condescending, a snob or not seeing the full picture.

    DanielSheets wrote:

    I don't care what you think

    Apparently you do else you wouldn't reply. :)

    D 1 Reply Last reply
    0
    • D DanielSheets

      This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

      S Offline
      S Offline
      shiprat
      wrote on last edited by
      #120

      I've used goto in C for coroutines in IP or serial protocols, mainly for manually crafting versions of the Protothread / Duffing machine trick. It's also handy in trampoline functions for tail recursion. It can be clearer and more consise than the alternatives.

      1 Reply Last reply
      0
      • C Chris Maunder

        In SQL - fairly often to jump to the error handler at the end of our sprocs. I'll admit there's no good reason we do this, since it's easy enough for us to avoid this with if statements, but it's a pattern used in our original code and so for consistency we stuck with it:

        Create Procedure MyProc as

        Begin Tran
        
        -- Do stuff...
        
        if @@error <> 0 goto errorHandler
        
        Commit Tran
        Return 0
        

        errorHandler:
        Rollback Tran
        Return 1

        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

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

        The problem with that view ("for consistency") is that you will forever be in the same programming mode. The form of the code should be

        if @@error <> 0
        BEGIN
        Rollback Tran
        Return 1
        END
        else
        BEGIN
        Commit Tran
        Return 0
        END

        In my world there are no exceptions! In C# I do accept the break statement within loops to allow an early escape. But no GOTOs. I programmed during the structured programming discussions in the 1970s. The amount of time required to comprehend GOTO filled code was enormous. And so easily fixed.

        Gus Gustafson

        1 Reply Last reply
        0
        • J Joe Woodbury

          DanielSheets wrote:

          You are convinced that goto's and ternary operators are examples of bad programming.

          Good grief. I didn't say ternary operators were bad; I said that since they are no longer needed in your specific code, they can be removed. What I learned was that the code

          text == "" ? "0" : ...

          does not throw an exception in C# if text is null. My view that gotos are bad is based on years of experience of finding bugs caused by their use (even in assembly where JMPs are a given and cause all sorts of problems when you aren't careful.) Condescension is in the eye of the beholder. In this case, you had a code review, extremely valid suggestions were made on how to improve it. You concede that much of what you are doing isn't needed, but insist on keeping it that way. That's fine, but don't go around blasting everyone for being condescending, a snob or not seeing the full picture.

          DanielSheets wrote:

          I don't care what you think

          Apparently you do else you wouldn't reply. :)

          D Offline
          D Offline
          DanielSheets
          wrote on last edited by
          #122

          I did have valid suggestions. And they were good suggestions. Was this part of your code review?

          Joe Woodbury wrote:

          "its" is spelled "it's" in this context, but it should probably read "it was".

          1 Reply Last reply
          0
          • S Stefan_Lang

            "correct use" depends on your definition of "correct". However, the main point is that even if at first it makes sense, or even simplifies code, it suffers from the lack of long-tem maintainability: Unlike other control statements, goto lacks an associated block of control, and thus makes it considerably harder to detect or verify the effect of any change you make to code that may or may not be executed, once or repeatedly, depending on goto statements in potentially several entirely different place(s). Goto would be much less of a problem if jump labels weren't self-declaring and global: if you could localize the use of a label like variable names, that would greatly restrict the potential (ab)use of goto commands, and thus ease the effort to understand the code and verify changes.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #123

            Stefan_Lang wrote:

            "correct use" depends on your definition of "correct". However, the main point is that even if at first it makes sense, or even simplifies code, it suffers from the lack of long-tem maintainability:

            Just to make it clear I have 40+ years of programming experience and I started before OO existed and used languages where one had to use goto along with using languages where goto didn't exist. And a number in between. I understand not only how goto can be misused but how any number of constructs can be used incorrectly. I also understand how absolutes are never that. (Where the former is a technical point and the latter part is not.) And that is the context in which I made my response.

            1 Reply Last reply
            0
            • K Kevin Marois

              Eddy Vluggen wrote:

              Readable code leads to maintainable projects. The better a project can be maintained, the more chances it will survive in the long run.

              .... which is completely offset by his use of 'goto'.

              If it's not broken, fix it until it is

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #124

              Kevin Marois wrote:

              .... which is completely offset by his use of 'goto'.

              Nope. The code as presented was readable and was maintainable. Given that maintenance is a proven and known expense then I would much rather have the code that was presented versus some esoteric trick that is technically correct but neither readable nor maintainable.

              1 Reply Last reply
              0
              • D DanielSheets

                This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

                O Offline
                O Offline
                obermd
                wrote on last edited by
                #125

                I can think of only one use for a Goto - tail recursion. A gosub or call immediately followed by a return can be replace by a Goto, saving stack space and the time needed to create/undo a stack frame.

                1 Reply Last reply
                0
                • R Rob Grainger

                  Nemanja Trifunovic wrote:

                  assembly, -C- and FORTRAN

                  FTFY - rarely used in C either. (Edit - C with strikethrough looked too much like a Euro symbol)

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #126

                  Rob Grainger wrote:

                  rarely used in C either.

                  Take a look at any non-trivial C code base (Linux kernel, for instance) and you'll see tons of goto statements.

                  utf8-cpp

                  1 Reply Last reply
                  0
                  • G Gary Huck

                    Been writing code since '85. Never used a goto in production code and I would have serious issue[s] with anyone who did.

                    C Offline
                    C Offline
                    Climate Turnip
                    wrote on last edited by
                    #127

                    Yes, quite. So you would have serious issue[s] with the people who wrote: jpeglib libsoap lua runtime mongoose gifencod etc. (just the examples I have at hand). Never had much of a problem maintaining/debugging a flow control that uses a label. Switch statements have probably caused more problems. But then I'm just being objective.

                    1 Reply Last reply
                    0
                    • K Keith Barrow

                      I can't ever remember needing it in c#. That said, it's been so long I doubt I'd spot a place where it might make things better.

                      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                      -Or-
                      A Dead ringer for Kate Winslett[^]

                      M Offline
                      M Offline
                      Member 4608898
                      wrote on last edited by
                      #128

                      This is when you would use a goto in C#. Basically a fall through in a switch statement.

                      1 Reply Last reply
                      0
                      • M Maximilien

                        I've not used it in a long time. There are a few of them in our legacy code, but no new code have them; they are mostly used for quick exit of a function to do cleanup.

                        Nihil obstat

                        H Offline
                        H Offline
                        hpcoder2
                        wrote on last edited by
                        #129

                        In C, or C++, the only situation I can recall using it for was to break out of multiple nested loops.

                        for (int i=0; i

                        Of course, in Java, Fortran (and presumably C#), one just uses a labelled break statement (exit statement in Fortran) for this purpose - main difference being that the label goes at the beginning of the loop being broken out of, rather than the end.

                        1 Reply Last reply
                        0
                        • L Lost User

                          Mauro Leggieri wrote:

                          For the other side, as an assembly programmer... I see JMPs everywhere hehehe.

                          ..simply because there's little alternative in assembly. Assumed we weren't talking about assembly, but higher-level languages - there are few people still working professionally with assembly.

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                          M Offline
                          M Offline
                          Mauro Leggieri
                          wrote on last edited by
                          #130

                          Days ago started to program a driver and remembered me that, altough in some places you can see try/leave/except blocks is modern code, because is programmed in plain C, "goto" is still used a lot mainly for cleanup purposes.

                          1 Reply Last reply
                          0
                          • J Joe Woodbury

                            I strongly disagree. This calls for splitting the function into two, if not more, pieces and/or rethinking the algorithm. I've seen code like this cause way too many bugs when something is introduced in the middle of BigFunction which doesn't get cleaned up at the end.

                            J Offline
                            J Offline
                            Jcmorin
                            wrote on last edited by
                            #131

                            You have the right to disagree, I'm saying this is a case where I see goto have it's usefulness. Yes you can split into multiple function but sometimes there is 10+ parameters involved and creating subfunction doesn't make it clearer. I respect your opinion and your strict mode but don't get me wrong... all languages [more or less] have the goto keyword or an equivalent.

                            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