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 5 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.
  • J Joe Woodbury

    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 Offline
    B Offline
    BobJanova
    wrote on last edited by
    #81

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

    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
      Rob Grainger
      wrote on last edited by
      #82

      DanielSheets wrote:

      It can make for cleaner code if used correctly.

      Post an example then, or it isn't so. I see you did, in another sub-thread, see my response there for a cleaner equivalent.

      1 Reply Last reply
      0
      • D DanielSheets
            public static void SaveChartData(List dataList)
            {
                int totalDelays = 0;
                int value = 0;
                string errorString = "";
                // Use a temporary file in case there are any parse errors.
                string tmpFilePath = applicationPath + "TmpChartData.csv";
                using (StreamWriter sw = new StreamWriter(tmpFilePath))
                {
                    foreach (DataGridClass dgc in dataList)
                    {
                        if (!int.TryParse((dgc.MATL.Equals("") ? "0" : dgc.MATL), out value))
                        {
                            errorString = dgc.MATL;
                            goto ParseError;
                        }
                        totalDelays += value;
                        if (!int.TryParse((dgc.EQUIP.Equals("") ? "0" : dgc.EQUIP), out value))
                        {
                            errorString = dgc.EQUIP;
                            goto ParseError;
                        }
                        totalDelays += value;
                        if (!int.TryParse((dgc.People.Equals("") ? "0" : dgc.People), out value))
                        {
                            errorString = dgc.People;
                            goto ParseError;
                        }
                        totalDelays += value;
                        if (!int.TryParse((dgc.Defects.Equals("") ? "0" : dgc.Defects), out value))
                        {
                            errorString = dgc.Defects;
                            goto ParseError;
                        }
                        totalDelays += value;
                        if (!int.TryParse((dgc.Other.Equals("") ? "0" : dgc.Other), out value))
                        {
                            errorString = dgc.Other;
                            goto ParseError;
                        }
                        totalDelays += value;
                        sw.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}",
                            dgc.Year, dgc.Month, dgc.Goal, dgc.Completions, totalDelays, dgc.to,
                            dgc.MATL, dgc.EQUIP, dgc.People, dgc.Defects, dgc.Other);
        
                        totalDelays = 0;
                        value = 0;
                    }
                }
                // If we got this far then there were no parse errors.
                File.Copy(tmpFilePath, chartData, true);
                return;
            ParseError:
                string msg = string.Format("Unable to parse data. Verify its entered correctly.\\r\\nValue = {0}", errorString);
                MessageBox.Sho
        
        R Offline
        R Offline
        Rob Grainger
        wrote on last edited by
        #83

        Quite simple, break the foreach loop into a separate method, returning a boolean. Replace goto's with return. Improved structure, documents itself (call it ParseDataGridList(List<DataGrid> dataList) ) Caller simply uses "if" on return value - if true { copies file } else { show message } Fixed.

        1 Reply Last reply
        0
        • D DanielSheets

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

          R Offline
          R Offline
          Rob Grainger
          wrote on last edited by
          #84

          DataGridClass

          1 Reply Last reply
          0
          • N Nemanja Trifunovic

            Real programmers (ones who code in languages like assembly, C and FORTRAN) use it all the time. Rest of us - rarely if ever.

            utf8-cpp

            R Offline
            R Offline
            Rob Grainger
            wrote on last edited by
            #85

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

              dusty_dex wrote:

              Many a word has been written about the use of multiple exits being bad practice.

              I think I understand the issue from all sides and I don't drink that kool-aid. The way I look at it, the goto takes linear flow and folds it back on itself, causing the programmer to think about an interleaved mesh of logic instead of a stream. I see the following items as presenting different concepts and approve/disapprove of each on their own merits: (1) goto backwards within a loop (2) goto forwards within a loop (3) goto forwards to exit handler/error handler at end of method (4) break statement in a loop (4a) bounded loop (4b) infinite loop (ie. exit in the middle) (5) break statement in a switch() (6) continue statement in a loop (7) return statement inside a loop (one only per method) (8) multiple return statements in a method History has shown that #1 has caused the most problems, and #2 follows closely. #3 seems to be the one that most people defend, and I think it has some merit. I have used #3 but I still avoid it whenever I can. I regularly use all of #4 through #8 and have no problem with them. Multiple returns in a method are really no different from a break statement in terms of how the programmer's brain processes the logic. Multiple returns cause an exit from a method (so there is no tortured logic flow), you can debug it (eg. you can put a breakpoint on it), do not fold logic back (so that you can get to a statement from multiple directions) and can reduce the amount of code in a method (less code is better code). Probably other things too - this is off the top of my head.

              -- Harvey

              R Offline
              R Offline
              Rob Grainger
              wrote on last edited by
              #86

              H.Brydon wrote:

              causing the programmer to think about an interleaved mesh of logic instead of a stream

              It also means that an optimiser is severely limited in the transformations it can make to code while retaining correctness - so will generally have a direct impact on performance too.

              1 Reply Last reply
              0
              • S Super Lloyd

                I hate all those anti-goto people! :mad:

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

                R Offline
                R Offline
                Rob Grainger
                wrote on last edited by
                #87

                That's OK, you're welcome to Goto Hell! ;-)

                1 Reply Last reply
                0
                • C C P User 3

                  None dare mention the fact that "break" and "goto" are really the same thing

                  R Offline
                  R Offline
                  Rob Grainger
                  wrote on last edited by
                  #88

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

                    Isyourspacebarfaulty? 'Go to' is two words.

                    M Offline
                    M Offline
                    MKJCP
                    wrote on last edited by
                    #89

                    In FORTRAN the space is not required. It will work either way. That is odd in itself.

                    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.

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

                      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();
                      }

                      T J 2 Replies Last reply
                      0
                      • J jschell

                        Collin Jasnoch wrote:

                        There is no "correct" usage of it in modern high level languages

                        I doubt the the absoluteness of that statement. It is likely there are few cases but even one refutes your statement.

                        S Offline
                        S Offline
                        Stefan_Lang
                        wrote on last edited by
                        #91

                        "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 1 Reply Last reply
                        0
                        • J Joe Woodbury

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

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

                          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 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
                            Cesar de Souza
                            wrote on last edited by
                            #93

                            It is often considered OK to call a goto from a deeply nested loop in order to terminate all loops altogether early, without the need of adding and maintaining several guard variables in the loop conditions. Of course if you can afford to place your loop in a separate function, you can also return from that, eliminating the goto but still doing the same.

                            Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Haar-feature Object Detection (With The Viola-Jones Framework) in C#

                            1 Reply Last reply
                            0
                            • A Andrew Torrance

                              If we are talking of C# then I cant think of a time I would ever use it , although I am somewhat biased as I have told everyone in my team that I would slaughter a family member if I ever found it in any code .

                              S Offline
                              S Offline
                              Stefan_Lang
                              wrote on last edited by
                              #94

                              Who would put family members in their code? :wtf:

                              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.

                                G Offline
                                G Offline
                                Gary Huck
                                wrote on last edited by
                                #95

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

                                T J C 3 Replies Last reply
                                0
                                • S S Douglas

                                  His second stop was the bar, then other places and to write this post. I am assuming he is still enjoying the effects of the local bar. :)


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

                                  G Offline
                                  G Offline
                                  Gary Huck
                                  wrote on last edited by
                                  #96

                                  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 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();
                                    }

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

                                    I think this example is pretty much the only justfiable use these days. I'm not ashamed to say that I use it this way occassionally. I much prefer that to deep nesting and multiple function returns. Multiple function returns often introduce bugs that could be avoided by using goto. But I'm wary of the religious wars against using goto so when I do use it I make sure I could justifty it in a code review.

                                    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.

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

                                      Good luck writing any assembly code without it.

                                      G J 2 Replies Last reply
                                      0
                                      • T tom1443

                                        Good luck writing any assembly code without it.

                                        G Offline
                                        G Offline
                                        Gary Huck
                                        wrote on last edited by
                                        #99

                                        Good luck getting me to write production assembly.

                                        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.

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

                                          see my example the message above your. If goto were that bad new language like C# would not have include it. It has a purpose, but it must be use in special case. Yes it can be avoided but something it the most clean solution.

                                          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