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

    Yes, you can make spaghetti code with goto's. However, you can just as easily make spaghetti code with degenerate loops, loops with insane stop criteria and weird booleans which you are forced to use as replacement. Also, just because some theoreticians said "goto is bad because it messes with the provability of the code" doesn't mean goto is actually bad. Who in their right mind actually proves that their code is correct? That's an insane amount of work, where you could just do some tests and "be reasonably sure" that it's fine. Chances are low that you're writing Mission Control code for an expensive space program. In other news, Knuth does not think that goto is evil, and wrote the paper "Structured Programming with Goto Statements" (use google).

    M Offline
    M Offline
    Mladen Jankovic
    wrote on last edited by
    #58

    harold aptroot wrote:

    provability of the code

    :laugh: There's a great Joel 's article on the subject. Talk at Yale: Part 1 of 3[^] And when all was said and done, she got to the end of the proof, and somehow was getting exactly the opposite result of the one that made sense, until that same graduate student pointed out where, 63 steps earlier, some bit had been accidentally flipped due to a little bit of dirt on the board, and all was well.

    [Genetic Algorithm Library] [Wowd]

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

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #59

      I used one a couple of weeks ago. I could have used some other mechanism but it was the perfect tool for that particular job, anything else would have added unecessary complexity and would probably have been optimised away into the same IL anyway. It's all part of being a programmer in my opinion - selecting the right tool [keyword]. Having said that, it's the first time I've used it in years!

      Dave

      If this helped, please vote & accept answer!

      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      1 Reply Last reply
      0
      • M Member 96

        Those who know when to use a goto do not tell, those who tell do not know. :) I've used them before, in probably 3 places out of millions of lines of code. In those 3 places it was entirely appropriate and called for in my opinion and I didn't even bother to find any other way to do it. It's a feature of the language and when it's right to use it you just know. Avoiding using it because some people still harbor a hatred for old style basic programming is about as silly as anything else in this world.


        Yesterday they said today was tomorrow but today they know better. - Poul Anderson

        M Offline
        M Offline
        Mladen Jankovic
        wrote on last edited by
        #60

        John C wrote:

        Those who know when to use a goto do not tell, those who tell do not know

        A bit like teenage sex?

        [Genetic Algorithm Library] [Wowd]

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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #61

          I am not religious about not using goto, I'd use it if it were ever truly useful, above and beyond the spagetti effect it creates. I've never once used it in C, C++, or C#.

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          1 Reply Last reply
          0
          • L leppie

            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

            S Offline
            S Offline
            Steve Wellens
            wrote on last edited by
            #62

            leppie wrote:

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

            And how do you put a breakpoint on Step3? If you can't put a breakpoint on a line of code, you lose maintainability.

            Steve Wellens

            L 1 Reply Last reply
            0
            • S Steve Wellens

              leppie wrote:

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

              And how do you put a breakpoint on Step3? If you can't put a breakpoint on a line of code, you lose maintainability.

              Steve Wellens

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

              Steve Wellens wrote:

              And how do you put a breakpoint on Step3?

              Either in Step3, or in the Do method...

              Steve Wellens wrote:

              If you can't put a breakpoint on a line of code, you lose maintainability.

              BS!

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

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

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

                Despite all preaching against them, gotos are nothing bad or evil. They just should be used sparingly and with consideration. When you have some deeply nested (spaghetti) code, a goto may be the only way to get out without making the whole thing even more complicated. Indeed you might also see a break as a goto with an implicit destination. Refactoring the code into separate methods may be a far better choice most of the time, but sometimes you need such a spaghetti monster and then gotos may be needed. But if somebody needs them regularily, that person should work a little on the coding style.

                A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                P 1 Reply 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

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

                  If you look at oldfashioned code you will see that it generally had a considerably smaller memory footprint and executed faster (in comparison). Good design obviously costs memory and some efficiency. With today's best practices one would not rally get far on a 8 bit CPU and 4k RAM. I don't rerally like the 'we have plenty of everything' attitude, however, only small and selected regions of the code need such a performance treatment and should then be well commented.

                  A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Yes, you can make spaghetti code with goto's. However, you can just as easily make spaghetti code with degenerate loops, loops with insane stop criteria and weird booleans which you are forced to use as replacement. Also, just because some theoreticians said "goto is bad because it messes with the provability of the code" doesn't mean goto is actually bad. Who in their right mind actually proves that their code is correct? That's an insane amount of work, where you could just do some tests and "be reasonably sure" that it's fine. Chances are low that you're writing Mission Control code for an expensive space program. In other news, Knuth does not think that goto is evil, and wrote the paper "Structured Programming with Goto Statements" (use google).

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

                    I second that. Most of the people that preach that goto is evil are the same ones over engineering their code into a mess no human can understand. Some professor told them that goto is bad so it has to be true. Now they have religion and everything is black and white. It's funny that multiple inheritance can be way worse but doesn't get anywhere near the bad press that goto does. I had an engineer tell me recently assembly language was unnecessary. Obviously he has never too done much embedded work on a little 8051. He also told me doing CORBA over a CANBus for a UI was practical. And of course he has given me the goto speech in the past. Should I believe him? Bottom line - it's more about the programmer than the language constructs.

                    L 1 Reply Last reply
                    0
                    • P Peter Mulholland

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

                      Pete

                      Z Offline
                      Z Offline
                      ZenAudio
                      wrote on last edited by
                      #67

                      Goto 10

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

                        P Offline
                        P Offline
                        pboucher
                        wrote on last edited by
                        #68

                        While I understand that sometimes a goto statement can solve a specific issue, I do not use it since the last time I programmed in GWBasic on my Sinclair 1000 computer (yes I am that old but still kicking). I believe there are always other better solutions than a GOTO and since I do not write OS code or device drivers, I do not need the extra performance that it can (sometimes) provide. IMHO, I believe that using GOTO in business/web application is an easy way out of a problem and its use should be discouraged. Train your brain into finding better solutions. Its much more fun.

                        Pierre Boucher 'Bien souvent on se rend coupable en négligeant d'agir, et non pas seulement en agissant.' - Marc Aurèle, empereur et philosophe romain.

                        1 Reply Last reply
                        0
                        • L Lost User

                          Despite all preaching against them, gotos are nothing bad or evil. They just should be used sparingly and with consideration. When you have some deeply nested (spaghetti) code, a goto may be the only way to get out without making the whole thing even more complicated. Indeed you might also see a break as a goto with an implicit destination. Refactoring the code into separate methods may be a far better choice most of the time, but sometimes you need such a spaghetti monster and then gotos may be needed. But if somebody needs them regularily, that person should work a little on the coding style.

                          A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                          P Offline
                          P Offline
                          pboucher
                          wrote on last edited by
                          #69

                          I completely agree with you. Sometimes the use of GOTO in the first place might be one of the cause of the spaghetti code.

                          Pierre Boucher 'Bien souvent on se rend coupable en négligeant d'agir, et non pas seulement en agissant.' - Marc Aurèle, empereur et philosophe romain.

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

                            B Offline
                            B Offline
                            BC3Tech
                            wrote on last edited by
                            #70

                            In my Comp Sci 201 class - object-oriented programming, first semester of college, we were given an F for ever using a goto statement.

                            E 1 Reply Last reply
                            0
                            • T tom1443

                              I second that. Most of the people that preach that goto is evil are the same ones over engineering their code into a mess no human can understand. Some professor told them that goto is bad so it has to be true. Now they have religion and everything is black and white. It's funny that multiple inheritance can be way worse but doesn't get anywhere near the bad press that goto does. I had an engineer tell me recently assembly language was unnecessary. Obviously he has never too done much embedded work on a little 8051. He also told me doing CORBA over a CANBus for a UI was practical. And of course he has given me the goto speech in the past. Should I believe him? Bottom line - it's more about the programmer than the language constructs.

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

                              Reminds me somehow of the people who keep insisting that "you can not beat the compiler" even though I keep proving them wrong :) They have a point that compiler could be smart, but in practice most aren't. Especially the ones for embedded systems.. they are godaweful, and IME most of the time you just can't afford to waste any performance/storage on crappy compiler output, they are scarce enough already.

                              1 Reply Last reply
                              0
                              • B BC3Tech

                                In my Comp Sci 201 class - object-oriented programming, first semester of college, we were given an F for ever using a goto statement.

                                E Offline
                                E Offline
                                Eaverae
                                wrote on last edited by
                                #72

                                And that's the way it should be :) I only used goto back in 1992, when I started to learn BASIC on a P2000T computer (6 years old) :D

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

                                  V Offline
                                  V Offline
                                  vbjay net
                                  wrote on last edited by
                                  #73

                                  Do you like spaghetti? If you use goto expect to get spaghetti code out of it. Following one line of code and having to jump to another point is not fun. When you are doing procedural code (like Qbasic) it was needed but now that you have functions and such it is a bad idea.

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

                                    F Offline
                                    F Offline
                                    Fabio Franco
                                    wrote on last edited by
                                    #74

                                    When all your .Net code is compiled to assembly code, all for's, while's, switches and if's will eventually become goto's (actually conditional "jumps"). Aside from that, the use of goto's just tells me bad design and coding, I've been programming for a while and never used goto's (despite the temptation in some situations). No situation really need goto's, they just need better design.

                                    1 Reply Last reply
                                    0
                                    • R Rage

                                      Well, he has a .45 in reach. Makes everybody agree with you.

                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #75

                                      I read that as "he has a .45 inch reach" and felt sorry for him for a moment.

                                      [Forum Guidelines]

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

                                        G Offline
                                        G Offline
                                        grgran
                                        wrote on last edited by
                                        #76

                                        Ok, say you are in one part of your code and you want to be in another part ... well there you "goto" :laugh: :-D

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

                                          E Offline
                                          E Offline
                                          Euhemerus
                                          wrote on last edited by
                                          #77

                                          Plenty of programming books I've read have always stated that it is best to avoid the GoTo statement because it increases the chances of producing 'spagetti' code. Having said that, I first cut my programming teeth on assembly language; and you just couldn't avoid the use of GoTo (Jmp) statement in assembly.

                                          I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

                                          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