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

    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.

    N Offline
    N Offline
    NAANsoft
    wrote on last edited by
    #101

    There is the "goto" in C# switch. That I have used lately. Otherwise I regard break and continue as instances of goto: break is the good guy (forwarding) where continue is the bad guy (backwarding(!)). / Normann

    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.

      C Offline
      C Offline
      Chris Boss
      wrote on last edited by
      #102

      GOTO is a vital tool for programmers wanting to develop high performance applications. In my primary commercial product (a GUI engine used by other programmers) I use it over a dozen times. Even the likes of Alexander Stepanov consider it useful, so I am in good company.

      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.

        C Offline
        C Offline
        CDMTJX
        wrote on last edited by
        #103

        Its been beaten into me over the years not to use GOTO's. Programming languages differ, but most have some sort of if / then / else that makes clearer programming than goto. Loops with leave / break statements, etc.. Even Fortran has these (I used it before it did)... I find indenting with GOTO replacements clearer to follow than hunting for GOTO's and their destination. Esp if you have huge functions. Even DOS batch scripting has if / then statements. Few years ago I cleaned up some messy batch scripts to make them readable...

        1 Reply Last reply
        0
        • R Rob Grainger

          No they are not. Break is designed to allow breaking out of a construct in a predictable, limited way. OK, in generated code, the result is still a branch, but one is much less likely to lead to abuses of control flow, particularly with later maintenance.

          C Offline
          C Offline
          C P User 3
          wrote on last edited by
          #104

          Point well taken. The misuse of Branches (or Jumps or whatever a given syntax may use) is probably one of the factors that led to the abandonment of assembly language; my true love. ICEs with a good 32 instruction backward trace capability will almost always provide the debug needed. But that's a moot point these days. The "consensus" has decreed, and so I (and thousands of others) suffer. Sad.

          1 Reply Last reply
          0
          • B BobJanova

            That ternary cannot throw. You're thinking of Java and its .equals nonsense. == won't throw for a null.

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

            Learned something new.

            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.

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

              The only place I can think where it might still be used in modern high-level languages is if one also adheres to the ancient (and out-dated?) principle that a function should have a single exit point. Sometimes making that happen without goto makes for some clunky, hard to read code with multi-level nests of IF blocks. My sig will confirm that I agree with you, though I haven't used goto since I abandoned QuickBasic some 15 years ago, and haven't written assembly in 20+.

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

              1 Reply Last reply
              0
              • D DanielSheets

                Joe Woodbury wrote:

                I would one step further since the ternary test is not only silly, it might throw an exception all on it's own due to text being null. If you KNOW something is "0", why parse it to 0? Why is an empty string valid? Is a null string valid?

                I dont know its going to be zero. It could be any number and it will never be null. An empty string is valid because a previous version of this code used them. This version doesnt.

                Joe Woodbury wrote:

                The code has other problems. Why create the file before you know whether there are any errors?

                Because there are terminals that watch for changes in this file. If a parse fails then I have a partially written data file. This will cause several other terminals to report errors.

                Joe Woodbury wrote:

                Why set totalDelays and value back to zero?

                Because totalDelays is for each individual class in dataList. If it's not set back to zero then it will add up across all of the classes in the list. You're making assumptions here. Setting delays to zero is unnecessary. That was left over and can be removed.

                Joe Woodbury wrote:

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

                Thanks for pointing that out. That grammatical error could cause the entire app to crash and burn. Good catch.

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

                DanielSheets wrote:

                I dont know its going to be zero. It could be any number and it will never be null.
                An empty string is valid because a previous version of this code used them. This version doesnt.

                int.TryParse((dgc.MATL.Equals("") ? "0" : dgc.MATL), out value))

                In the case of empty string, you know the result will be zero. If this isn't valid for this version, refactor the code and remove the check.

                DanielSheets wrote:

                Because there are terminals that watch for changes in this file. If a parse fails then I have a partially written data file. This will cause several other terminals to report errors.

                You miss my point. You don't need to create the file until you have added up the total count. You can still keep the rename in for the purposes you stated.

                DanielSheets wrote:

                Because totalDelays is for each individual class in dataList. If it's not set back to zero then it will add up across all of the classes in the list. You're making assumptions here.

                Huh? Oh yeah, I was right:

                DanielSheets wrote:

                Setting delays to zero is unnecessary. That was left over and can be removed.

                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?

                D 1 Reply Last reply
                0
                • J Jcmorin

                  The goto is not required, you can always skip around with variable but sometime it's the best solution. In this example the goto is a clear simple example, remove it and you introduce less readable and more complexity.

                  function BigFunction() {
                  for (int i =0; i < 100; i++) {
                  while(true) {
                  if (...) {
                  goto DO_SOMETHING_AT_THE_END;
                  }
                  }
                  }

                  DO_SOMETHING_AT_THE_END:

                  DoSomething();
                  }

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

                  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 1 Reply Last reply
                  0
                  • T tom1443

                    Good luck writing any assembly code without it.

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

                    I wrote assembly for years and never used "goto". :)

                    T 1 Reply Last reply
                    0
                    • G Gary Huck

                      My guess is he goes to those places often and with reason. Thus, other block controls would be more useful. E.g., while he is not at work he is in the bar, etc.

                      S Offline
                      S Offline
                      S Douglas
                      wrote on last edited by
                      #110

                      :laugh:


                      Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

                      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.

                        A Offline
                        A Offline
                        Al Chak
                        wrote on last edited by
                        #111

                        There is not an assembler program without GOTO. Fortran without GOTO is too hard. But in the real programming - GOTO is absolute unusable in all situations. Last 25 years, I did not write GOTO( expect assembler) and removed each one from reused code.

                        1 Reply Last reply
                        0
                        • J Joe Woodbury

                          I wrote assembly for years and never used "goto". :)

                          T Offline
                          T Offline
                          tom1443
                          wrote on last edited by
                          #112

                          That is why I alias goto to JMP!

                          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

                            A Offline
                            A Offline
                            Al Chak
                            wrote on last edited by
                            #113

                            :confused:

                            if (then) else

                            - Is this "too match" for SQL?

                            1 Reply Last reply
                            0
                            • J Joe Woodbury

                              DanielSheets wrote:

                              I dont know its going to be zero. It could be any number and it will never be null.
                              An empty string is valid because a previous version of this code used them. This version doesnt.

                              int.TryParse((dgc.MATL.Equals("") ? "0" : dgc.MATL), out value))

                              In the case of empty string, you know the result will be zero. If this isn't valid for this version, refactor the code and remove the check.

                              DanielSheets wrote:

                              Because there are terminals that watch for changes in this file. If a parse fails then I have a partially written data file. This will cause several other terminals to report errors.

                              You miss my point. You don't need to create the file until you have added up the total count. You can still keep the rename in for the purposes you stated.

                              DanielSheets wrote:

                              Because totalDelays is for each individual class in dataList. If it's not set back to zero then it will add up across all of the classes in the list. You're making assumptions here.

                              Huh? Oh yeah, I was right:

                              DanielSheets wrote:

                              Setting delays to zero is unnecessary. That was left over and can be removed.

                              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?

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

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

                                C Offline
                                C Offline
                                Cj Welborn
                                wrote on last edited by
                                #115

                                Twenty years ago i was 10, and learning to program in QBASIC. That was the last time I used GOTO.

                                1 Reply Last reply
                                0
                                • S Super Lloyd

                                  I am not afraid to say I used it at least 4 or 5 times! (in the last 10 years!...) Even once recently! I hate the mindless peer pressure against it, use it even it's ugly if you like! Use whatever makes your code more beautiful! ^^ Just so you know, the (mindless violent) hate against it is based on the following argument: "it's not maintainable" i.e. "it break the flow of the code which should be otherwise obvious" That much is true, long methods with goto label hidden 300 line below are big traps. But this is true of 300 lines method without goto too!!! So, shortly, use it if it's the shorter more expressive solution. If someone doesn't like it, suggest them to fix the code. And choose the most expressive readable code between theirs and yours after that! ;P Anyway, when one use goto? Err... truthfully only one C# exemple comes to my mind (apart switch): how to break out simple of multiple nest loop

                                  for ()
                                  for(..)
                                  for(..)
                                  {
                                  if(condition)
                                  goto exit_loop;
                                  }
                                  exit_loop:;

                                  Just so you know, a typical C goto will be for clean up, as in

                                  if (success1) {...}
                                  else goto failure
                                  If (sucess2) { ...}
                                  goto failure
                                  ...
                                  return;
                                  failure:

                                  but in C# this is more nicely expressed with try {} catch {} finally {} which doesn't need any goto

                                  My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                                  A Offline
                                  A Offline
                                  Al Chak
                                  wrote on last edited by
                                  #116

                                  Oh, this is nice example for bad logic.

                                  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.

                                    P Offline
                                    P Offline
                                    patbob
                                    wrote on last edited by
                                    #117

                                    I grew up with gotos, and saw their abuse first hand. Then some luminary someplace realized they were being abused, so decreed them to be evil. Now I keep them hidden away in my basement, away from prying eyes. I don't abuse my gotos like other programmers did, and see that they can still be useful, but I'm too scared of what the other programmers would say to let them out to frolic once in a while. Poor abused little gotos ;(

                                    We can program with only 1's, but if all you've got are zeros, you've got nothing.

                                    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.

                                      M Offline
                                      M Offline
                                      MainFrameMan_ALIVE_AND_WELL
                                      wrote on last edited by
                                      #118

                                      As a matter of fact, I have to MainTainTheInsane Ball of string of GoTo's in legacy applications that were written decades ago by someone who is long gone. I see it all over and really had a fit when I had to code around them. But since I have been learning COBOL of all flavors, I can now see that it was all they had back then; think of Sequential programing real hard with no bells and whistles like the modern languages have now. After figuring out what one of these old legacy apps does, I have to admire the person/s who had written them; they had to be a lot more creative with so little to work with than what programmers have today to work with. Actually, I have seen some really cool logic using the GoTo; and again, some really insane WTF! stuff. Back in the days, yeah, it was necessary. Todays programming, NOT :)

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