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 loops

goto loops

Scheduled Pinned Locked Moved The Lounge
csharpquestion
88 Posts 45 Posters 0 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.
  • L LucianPopescu

    Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

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

    This article/blog[^] Shows situations where goto statements can be used to greater effect than other soloutions although I must confess I can see better ways to code some of the examples, that the author ignores. I was always taught to avoid goto's at all costs and have always stuck to this advice, however, like you, I have noticed collegues use them a lot.

    P R L 4 Replies Last reply
    0
    • P Pete OHanlon

      OK - to clarify. What you have asked here is more of a programming philosophy question than a programming question. Others ask these in the Lounge and get away with them so I don't see why you shouldn't. I have never encountered a situation in .NET where a goto makes my job easier. Generally, a well structured application with small, self-contained methods should have no need for a goto. When I see them, it's generally an indication that somebody has landed themselves in an architectural muddle and they can't see the way out of the morass. This is generally when they have loops nested within loops nested within loops. The way I tend to look at this, if you've got yourself this deep into loopfuggery, it's an indication that you are using the wrong loops.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      M Offline
      M Offline
      Mario Luis
      wrote on last edited by
      #8

      Never used it in c#.

      1 Reply Last reply
      0
      • L LucianPopescu

        Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

        M Offline
        M Offline
        Mark_Wallace
        wrote on last edited by
        #9

        That you call them "goto loops" speaketh volumes. Like every other programming tool, the goto has its uses. It is mostly used underwater, by the things you use instead of gotos. It can also, like every other programming tool, be used stupidly by bad programmers.

        I wanna be a eunuchs developer! Pass me a bread knife!

        1 Reply Last reply
        0
        • L Lost User

          This article/blog[^] Shows situations where goto statements can be used to greater effect than other soloutions although I must confess I can see better ways to code some of the examples, that the author ignores. I was always taught to avoid goto's at all costs and have always stuck to this advice, however, like you, I have noticed collegues use them a lot.

          P Offline
          P Offline
          Peter Mulholland
          wrote on last edited by
          #10

          Yep, I was taught to avoid gotos also cause they make the code harder to read and ifs, selects, whiles and fors can all get you the same results and they are easier to read. However I have noticed some of the newbies using gotos here (in my place of work) also. It appears they have come back into fashion. Maybe because they were eradicated so well that the newbies aren't being taught how bad code can get with injudicious use of gotos?

          Pete

          P 1 Reply Last reply
          0
          • L LucianPopescu

            Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

            J Offline
            J Offline
            Johnny J
            wrote on last edited by
            #11

            NEVER, EVER use GoTo - It's generally very bad programming and makes the code harder to read. That goes for both C# and VB.NET.

            M 1 Reply Last reply
            0
            • L LucianPopescu

              Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

              R Offline
              R Offline
              Rocky Moore
              wrote on last edited by
              #12

              Every now rare once and a while (althought it has been probably close to a decade for me ;) ) it can be useful. The common clause is that it is probably better to analyze your code and see if it would not be cleaner and easier to read another way.

              Rocky <>< Recent Blog Post: The Arrogant Apple!

              1 Reply Last reply
              0
              • P Peter Mulholland

                Yep, I was taught to avoid gotos also cause they make the code harder to read and ifs, selects, whiles and fors can all get you the same results and they are easier to read. However I have noticed some of the newbies using gotos here (in my place of work) also. It appears they have come back into fashion. Maybe because they were eradicated so well that the newbies aren't being taught how bad code can get with injudicious use of gotos?

                Pete

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #13

                Maybe they are reading unoptimised reflector code. Goto is commonplace there.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                1 Reply Last reply
                0
                • L Lost User

                  This article/blog[^] Shows situations where goto statements can be used to greater effect than other soloutions although I must confess I can see better ways to code some of the examples, that the author ignores. I was always taught to avoid goto's at all costs and have always stuck to this advice, however, like you, I have noticed collegues use them a lot.

                  P Offline
                  P Offline
                  Peter Mulholland
                  wrote on last edited by
                  #14

                  Ok, surely the first and last examples here can be done using return in a try-finally block?

                  Pete

                  L 1 Reply Last reply
                  0
                  • L LucianPopescu

                    Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

                    R Offline
                    R Offline
                    realJSOP
                    wrote on last edited by
                    #15

                    No serious programmer uses a goto. EDIT (After the 1 vote)---------------- Okay - maybe we all do - when we goto the bathroom...

                    .45 ACP - because shooting twice is just silly
                    -----
                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                    modified on Monday, July 5, 2010 10:22 AM

                    P J M 3 Replies Last reply
                    0
                    • L LucianPopescu

                      ok then... sorry for posting.. i give up.

                      R Offline
                      R Offline
                      realJSOP
                      wrote on last edited by
                      #16

                      Lucian-aSterX wrote:

                      i give up.

                      Why? You're not French...

                      .45 ACP - because shooting twice is just silly
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                      R 1 Reply Last reply
                      0
                      • J Johnny J

                        NEVER, EVER use GoTo - It's generally very bad programming and makes the code harder to read. That goes for both C# and VB.NET.

                        M Offline
                        M Offline
                        moon_stick
                        wrote on last edited by
                        #17

                        While advice like this is doled out to inexperienced developers, once you reach a certain level of proficiency it becomes poor or useless advice. I was told by uni lecturers as I was doing my degree that inexperienced developers can try to use gotos as a means for everything, rather than thinking about more concise loop construct which is why they're advised to steer clear. However, this doesn't make the advice sound - it's more of a teaching aid to guide in the right direction. To give a specific example, using a goto as an exit clause in a set of nested 'for' loops is probably the most efficient way of forcing an early exit, and is probably the easiest to read. Goto is perfectly acceptably - just try not to overuse it.

                        Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                        R 1 Reply Last reply
                        0
                        • P Peter Mulholland

                          Ok, surely the first and last examples here can be done using return in a try-finally block?

                          Pete

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

                          I did say I can see better soloutions that the author chooses to ignore.

                          P D 2 Replies Last reply
                          0
                          • M moon_stick

                            While advice like this is doled out to inexperienced developers, once you reach a certain level of proficiency it becomes poor or useless advice. I was told by uni lecturers as I was doing my degree that inexperienced developers can try to use gotos as a means for everything, rather than thinking about more concise loop construct which is why they're advised to steer clear. However, this doesn't make the advice sound - it's more of a teaching aid to guide in the right direction. To give a specific example, using a goto as an exit clause in a set of nested 'for' loops is probably the most efficient way of forcing an early exit, and is probably the easiest to read. Goto is perfectly acceptably - just try not to overuse it.

                            Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                            R Offline
                            R Offline
                            realJSOP
                            wrote on last edited by
                            #19

                            Did you vote him a 1?

                            .45 ACP - because shooting twice is just silly
                            -----
                            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                            -----
                            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                            M 1 Reply Last reply
                            0
                            • R realJSOP

                              Did you vote him a 1?

                              .45 ACP - because shooting twice is just silly
                              -----
                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                              -----
                              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                              M Offline
                              M Offline
                              moon_stick
                              wrote on last edited by
                              #20

                              Yes - I scored the post in a way I saw fit and gave reasons in the response I posted. Which I believe is actually the polite way of doing things!

                              Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                              R 1 Reply Last reply
                              0
                              • L Lost User

                                I did say I can see better soloutions that the author chooses to ignore.

                                P Offline
                                P Offline
                                Peter Mulholland
                                wrote on last edited by
                                #21

                                :-D This is a discussion and I'm not claiming a try-finally block would be a better solution.

                                Pete

                                Z 1 Reply Last reply
                                0
                                • L Lost User

                                  This article/blog[^] Shows situations where goto statements can be used to greater effect than other soloutions although I must confess I can see better ways to code some of the examples, that the author ignores. I was always taught to avoid goto's at all costs and have always stuck to this advice, however, like you, I have noticed collegues use them a lot.

                                  R Offline
                                  R Offline
                                  Rage
                                  wrote on last edited by
                                  #22

                                  pompeyboy3 wrote:

                                  to greater effect than other soloutions although I must confess I can see better ways to code some of the examples

                                  to greater effect than the other solutions he provided, but not greater than real good solutions. As you started to point out, you can write his example in perfectly good code without requiring to the goto instruction. IMHO, this blog entry is rather poor.

                                  S 1 Reply Last reply
                                  0
                                  • M moon_stick

                                    Yes - I scored the post in a way I saw fit and gave reasons in the response I posted. Which I believe is actually the polite way of doing things!

                                    Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                                    R Offline
                                    R Offline
                                    realJSOP
                                    wrote on last edited by
                                    #23

                                    Wow - looks like someone didn't agree with you, and didn't say why.

                                    .45 ACP - because shooting twice is just silly
                                    -----
                                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                    -----
                                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                    M P 2 Replies Last reply
                                    0
                                    • L Lost User

                                      This article/blog[^] Shows situations where goto statements can be used to greater effect than other soloutions although I must confess I can see better ways to code some of the examples, that the author ignores. I was always taught to avoid goto's at all costs and have always stuck to this advice, however, like you, I have noticed collegues use them a lot.

                                      L Offline
                                      L Offline
                                      leppie
                                      wrote on last edited by
                                      #24

                                      Funny how no-one even the comments could come up with a really good alternative (I agree for simplicity, the && is approach is best). How would I do it?

                                      bool Do(params Func<bool>[] args)
                                      {
                                      foreach (var a in args)
                                      if (!a())
                                      return false;
                                      return true;
                                      }
                                      ...

                                      if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7))
                                      Console.WriteLine("Brillant!");

                                      xacc.ide
                                      IronScheme - 1.0 RC 1 - out now!
                                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition

                                      R S 2 Replies Last reply
                                      0
                                      • R realJSOP

                                        Lucian-aSterX wrote:

                                        i give up.

                                        Why? You're not French...

                                        .45 ACP - because shooting twice is just silly
                                        -----
                                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                        -----
                                        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                        R Offline
                                        R Offline
                                        Rage
                                        wrote on last edited by
                                        #25

                                        We did not give up. We made a tactical counter march and just never had any strategic opportunity to go back to the front.

                                        1 Reply Last reply
                                        0
                                        • R realJSOP

                                          Wow - looks like someone didn't agree with you, and didn't say why.

                                          .45 ACP - because shooting twice is just silly
                                          -----
                                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                          -----
                                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                          M Offline
                                          M Offline
                                          moon_stick
                                          wrote on last edited by
                                          #26

                                          It's not something I'm going to lose any sleep over!

                                          Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                                          J 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