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

    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 J 2 Replies 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
      GuyThiebaut
      wrote on last edited by
      #24

      That's a pattern I have come across in Oracle PL/SQL and it does the job well. Of course one can use an if, as you mention, but if it is used judiciously it works well and does not break the logic any more than an else does.

      “That which can be asserted without evidence, can be dismissed without evidence.”

      ― Christopher Hitchens

      1 Reply Last reply
      0
      • D DanielSheets

        For what part? Why would I need to use exception handling when thats exactly what TryParse is used for (assuming thats what you're talking about)?

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

        ..on how to throw an exception. I wouldn't even expect the "if's", but rather a loop; and have each condition in a separate class.

                using (StreamWriter sw = new StreamWriter(tmpFilePath))
                {
                    foreach (DataGridClass dgc in dataList)
                    {
                        foreach (var thingToTest in ParsesClasses)
                        {
                             Results.Add( thingToTest (dgc) );
                        }
        

        Wrap it in a try-catch, and done.

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

        D 1 Reply Last reply
        0
        • L Lost User

          ..on how to throw an exception. I wouldn't even expect the "if's", but rather a loop; and have each condition in a separate class.

                  using (StreamWriter sw = new StreamWriter(tmpFilePath))
                  {
                      foreach (DataGridClass dgc in dataList)
                      {
                          foreach (var thingToTest in ParsesClasses)
                          {
                               Results.Add( thingToTest (dgc) );
                          }
          

          Wrap it in a try-catch, and done.

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

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

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

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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #27

            I haven't used a "goto" since.....let's see now.....1995, in some assembly language I was writing for a graphics library. This s, if you call any flavor of JMP a "goto". Come to think of it, that's probably around the last time I've ever used the keyword "goto" in any higher languages.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            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

              D Offline
              D Offline
              Dexterus
              wrote on last edited by
              #28

              For this same reason it was used in our C code (kernel, various drivers). Not had to use it yet but I can see some uses if you want the CPU/compiler to execute/optimize properly.

              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.

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

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

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

                  depends on the language. sometimes, there is no way to avoid it.

                  image processing toolkits | batch image processing

                  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
                    glennPattonWork3
                    wrote on last edited by
                    #31

                    I have always thought goto was the equlivant of the assembly language jmp (blocking?), if it is I used one this morning. Goto is also acceptable in RTOS situations (which Windows isn't) if you want the valve to close now and not later. Glenn

                    1 Reply Last reply
                    0
                    • B BobJanova

                      Isyourspacebarfaulty? 'Go to' is two words.

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

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

                        Use it in .bat files all the time.

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

                        wizardzz wrote:

                        in .bat files all the time.

                        I second that.


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

                          J Offline
                          J Offline
                          J4amieC
                          wrote on last edited by
                          #34

                          If that's T-SQL in anything resembling modern SQL Server, there are much, much better constructs for error handling now.

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

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

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

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