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.

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

    Well as an assembly programmer I really don't have much choice. And as for higher level languages, sure, though rarely. There are people who religiously avoid it, but that's just silly. We must remember the reason it is seen as bad, and not open a witchhunt. Introducing a boolean variable just to quit out of a non-enclosing loop makes readability/understandability worse, not better, and refactoring an inner loop into its own function just so you can return out of it creates a bunch of tightly-coupled functions that do nothing useful on their own. Besides, that return would essentially be a goto.

    1 Reply Last reply
    0
    • D dusty_dex

      That looks bad to me. Aren't you supposed to use

      break ;

      to jump out of loops? Exception handling will usually bog down the handled block of code.

      Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

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

      Unfortunately that Java feature is not in C#.

      D 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

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #38

        Honestly, I have written at least 1 million lines of code since I have used a goto. Although I do remember using a few gotos in the 1990s. Also long gone from my coding is writing x86 assembly code.

        John

        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.

          R Offline
          R Offline
          Ron Anders
          wrote on last edited by
          #39

          Me: goto Me;

          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.

            K Offline
            K Offline
            kmoorevs
            wrote on last edited by
            #40

            In one of our legacy apps (VB) the goto is used to apply database updates based on the exe version. ErrorHandlers: seperate the update logic for each version and are arranged from top to bottom so that code execution always 'fall through' to the bottom. It may me wrong but it works!

            "Go forth into the source" - Neal Morse

            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.

              R Offline
              R Offline
              RugbyLeague
              wrote on last edited by
              #41

              I put it into my own language just because I wanted to see if I could write a program all in one function which started at the bottom and worked its way to the top. I am not sure why, one of those days. I then immediately removed it from the language spec although the code for it is still in the compiler source - just commented out.

              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.

                H Offline
                H Offline
                H Brydon
                wrote on last edited by
                #42

                I only use it nowadays in assembly (no choice) and scripting languages that don't have good structured programming constructs (ie. if/then/else etc.). I learned programming in the days of early Fortran IV (and actually regressed a little on an older machine with Fortran IID) that only had arithmetic if[^] and computed goto[^] for conditional logic flow. I really came to appreciate the evils of the 'goto' statement and never use them unless there is no other choice. If I use a goto, it will always transfer control downwards, never up. And yes, I use break and continue, and multiple returns in methods.

                -- Harvey

                D 1 Reply Last reply
                0
                • L Lost User

                  Unfortunately that Java feature is not in C#.

                  D Offline
                  D Offline
                  dusty_dex
                  wrote on last edited by
                  #43

                  Wow! what an oversight. It sure makes life easier.

                  Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

                  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.

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #44

                    .Net framework uses that. They just built some fancy keywords that do the same thing but have a different name. For instance, continue.

                    "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

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

                      K Offline
                      K Offline
                      Kevin Marois
                      wrote on last edited by
                      #45

                      goto... Who uses it? Only those programmers who want to be slowly boiled in hot oil

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

                      K 1 Reply Last reply
                      0
                      • D dan sh

                        .Net framework uses that. They just built some fancy keywords that do the same thing but have a different name. For instance, continue.

                        "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

                        K Offline
                        K Offline
                        Kevin Marois
                        wrote on last edited by
                        #46

                        If you think the 'continue' keyword is anything at all like 'goto', you need to go back to programming 101.

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

                        D 1 Reply Last reply
                        0
                        • D DanielSheets

                          Thats swift. I'll be the first to admit that I dont have the experience or knowledge of 90% of the people here. Interesting.

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

                          DanielSheets wrote:

                          I'll be the first to admit that I dont have the experience or knowledge of 90% of the people here.

                          The fact that you care about readability proves that you can't be in the bottom 10% :) Readable code leads to maintainable projects. The better a project can be maintained, the more chances it will survive in the long run.

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

                          K 1 Reply Last reply
                          0
                          • H H Brydon

                            I only use it nowadays in assembly (no choice) and scripting languages that don't have good structured programming constructs (ie. if/then/else etc.). I learned programming in the days of early Fortran IV (and actually regressed a little on an older machine with Fortran IID) that only had arithmetic if[^] and computed goto[^] for conditional logic flow. I really came to appreciate the evils of the 'goto' statement and never use them unless there is no other choice. If I use a goto, it will always transfer control downwards, never up. And yes, I use break and continue, and multiple returns in methods.

                            -- Harvey

                            D Offline
                            D Offline
                            dusty_dex
                            wrote on last edited by
                            #48

                            Many a word has been written about the use of multiple exits being bad practice. Perhaps the next discussion should be on the use of many returns.

                            Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

                            H 1 Reply Last reply
                            0
                            • L Lost User

                              DanielSheets wrote:

                              I'll be the first to admit that I dont have the experience or knowledge of 90% of the people here.

                              The fact that you care about readability proves that you can't be in the bottom 10% :) Readable code leads to maintainable projects. The better a project can be maintained, the more chances it will survive in the long run.

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

                              K Offline
                              K Offline
                              Kevin Marois
                              wrote on last edited by
                              #49

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

                                E Offline
                                E Offline
                                emartinho
                                wrote on last edited by
                                #50

                                Wow. I expected more of a flame war. Bring on the rants!!!!

                                S 1 Reply Last reply
                                0
                                • K Kevin Marois

                                  goto... Who uses it? Only those programmers who want to be slowly boiled in hot oil

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

                                  K Offline
                                  K Offline
                                  kmoorevs
                                  wrote on last edited by
                                  #51

                                  I'd prefer to be fried... :laugh:

                                  "Go forth into the source" - Neal Morse

                                  1 Reply Last reply
                                  0
                                  • B BobJanova

                                    That's horrible. Repeated code, and mixing of data management and UI code. It's crying out for throwing exceptions which you catch in the UI code, and a method

                                    int parseNumericValue(String text){
                                    int result;
                                    if(!int.TryParse(text == "" ? "0" : text, out result))
                                    throw new DataFormatException("Unable to parse data. Verify its entered correctly.\r\nValue = "+text);
                                    return result;
                                    }

                                    (and that's if the default exception from Parse doesn't do the job which it probably does). This is translated VB. And that's not a good thing.

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

                                    Where do you see UI code in the snippet I posted?

                                    J R 2 Replies Last reply
                                    0
                                    • K Kevin Marois

                                      If you think the 'continue' keyword is anything at all like 'goto', you need to go back to programming 101.

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

                                      D Offline
                                      D Offline
                                      dan sh
                                      wrote on last edited by
                                      #53

                                      I beg to differ here. Check out the IL generated for the two methods:

                                      void Test()
                                      {
                                      while (true)
                                      {
                                      string str = "Something";

                                                  if (str.Length != 10)
                                                  {
                                                      continue;
                                                  }
                                      
                                              }
                                          }
                                      
                                          void Test2() {
                                          x:
                                              while (true)
                                              {
                                                  string str = "Something";
                                      
                                                  if (str.Length != 10)
                                                  {
                                                      goto x;
                                                  }
                                      
                                              }
                                          }
                                      

                                      //without GOTO .method private hidebysig instance void Test() cil managed { // Code size 32 (0x20) .maxstack 2 .locals init ([0] string str, [1] bool CS$4$0000) IL_0000: nop IL_0001: br.s IL_001c IL_0003: nop IL_0004: ldstr "Something" IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0010: ldc.i4.s 10 IL_0012: ceq IL_0014: stloc.1 IL_0015: ldloc.1 IL_0016: brtrue.s IL_001b IL_0018: nop IL_0019: br.s IL_001c IL_001b: nop IL_001c: ldc.i4.1 IL_001d: stloc.1 IL_001e: br.s IL_0003 } // end of method Proof::Test //With GOTO .method private hidebysig instance void Test2() cil managed { // Code size 32 (0x20) .maxstack 2 .locals init ([0] string str, [1] bool CS$4$0000) IL_0000: nop IL_0001: br.s IL_001c IL_0003: nop IL_0004: ldstr "Something" IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0010: ldc.i4.s 10 IL_0012: ceq IL_0014: stloc.1 IL_0015: ldloc.1 IL_0016: brtrue.s IL_001b IL_0018: nop IL_0019: br.s IL_0001 IL_001b: nop IL_001c: ldc.i4.1 IL_001d: stloc.1 IL_001e: br.s IL_0003 } // end of method Proof::Test2

                                      "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

                                      K S 2 Replies 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.

                                        G Offline
                                        G Offline
                                        Gary Wheeler
                                        wrote on last edited by
                                        #54

                                        I've not used it in 15 years or so, in any of my C# or C++ applications. There are too many better alternatives to goto in those languages. The last time I used goto was in C in the 90's, when the compilers I was using didn't (reliably) support exceptions.

                                        Software Zen: delete this;

                                        M 1 Reply Last reply
                                        0
                                        • B BobJanova

                                          That's horrible. Repeated code, and mixing of data management and UI code. It's crying out for throwing exceptions which you catch in the UI code, and a method

                                          int parseNumericValue(String text){
                                          int result;
                                          if(!int.TryParse(text == "" ? "0" : text, out result))
                                          throw new DataFormatException("Unable to parse data. Verify its entered correctly.\r\nValue = "+text);
                                          return result;
                                          }

                                          (and that's if the default exception from Parse doesn't do the job which it probably does). This is translated VB. And that's not a good thing.

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

                                          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? The code has other problems. Why create the file before you know whether there are any errors? Why set totalDelays and value back to zero? "its" is spelled "it's" in this context, but it should probably read "it was".

                                          B D 2 Replies 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