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. for those of you purists that don't like break, continue and goto

for those of you purists that don't like break, continue and goto

Scheduled Pinned Locked Moved The Lounge
question
65 Posts 21 Posters 1 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

    If you enjoy pain try mixing throw with interop. (sure way to properly destroy documents and pst files.)

    Message Signature (Click to edit ->)

    H Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #18

    oooh. Another place i like to throw is in static constructors, just to get absolutely everyone's BAC up. :laugh: I'm kidding. I'm not a sadist.

    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

    1 Reply Last reply
    0
    • H honey the codewitch

      why do this?

      for(int i = 0;i
      instead of

      for(int i = 0;i

      hengh?? why you still use break?

      :laugh:

      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

      N Offline
      N Offline
      Nand32
      wrote on last edited by
      #19

      At times, the purpose of the search might be just to find the index of the valueToFind. Break keeps the index safe?

      (JS)
      var arr =[0,1,2,3,4,5];
      var valueToFind = 3;

      for(i=0;i

      H 1 Reply Last reply
      0
      • G GuyThiebaut

        honey the codewitch wrote:

        My guess is that would put you in the minority.

        I agree, my experience is that as a generalisation us developers have very strong opinions. At the age of 49 I have found that I no longer have the desire to get into arguments over this sort of thing - as in six months time the 'best practise' recommendation will in all likelihood have switched.

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

        ― Christopher Hitchens

        H Offline
        H Offline
        honey the codewitch
        wrote on last edited by
        #20

        an ironic amen to that. I'm a bit younger, but grew tired of the holy rolling, compounded with me leaving the field and no longer being required to write "team" code, I can just do my own thing As a result I've become a bit more buddhist when it comes to most things coding. There are still certain hills I'll die on though, I admit.

        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

        1 Reply Last reply
        0
        • H honey the codewitch

          ugh, VB. I use goto in some of my code. Perfectly acceptable place to use GOTO - generated state machine code:

          public static bool AcceptsByte(Grimoire.ParseContext pc)
          {
          pc.EnsureStarted();
          if (-1 == pc.Current) return false;
          if ((48 == pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s1;
          }
          if ((49 == pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s2;
          }
          if ((50 == pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s4;
          }
          if ((51 <= pc.Current && 57 >= pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s3;
          }
          return false;
          AcceptsByte_s1:
          if (-1 == pc.Current) return true;
          return -1 == pc.Advance();
          AcceptsByte_s2:
          if (-1 == pc.Current) return true;
          if ((48 <= pc.Current && 57 >= pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s3;
          }
          return -1 == pc.Advance();
          AcceptsByte_s3:
          if (-1 == pc.Current) return true;
          if ((48 <= pc.Current && 57 >= pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s1;
          }
          return -1 == pc.Advance();
          AcceptsByte_s4:
          if (-1 == pc.Current) return true;
          if ((48 <= pc.Current && 52 >= pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s3;
          }
          if ((53 == pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s5;
          }
          if ((54 <= pc.Current && 57 >= pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s1;
          }
          return -1 == pc.Advance();
          AcceptsByte_s5:
          if (-1 == pc.Current) return true;
          if ((48 <= pc.Current && 52 >= pc.Current))
          {
          pc.Advance();
          goto AcceptsByte_s1;
          }
          return -1 == pc.Advance();
          }

          but then I wouldn't write that code by hand. Too error prone.

          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

          N Offline
          N Offline
          Nand32
          wrote on last edited by
          #21

          Yep, goto on a lengthier code is definitely not meant for the human eyes. :)

          H 1 Reply Last reply
          0
          • N Nand32

            Yep, goto on a lengthier code is definitely not meant for the human eyes. :)

            H Offline
            H Offline
            honey the codewitch
            wrote on last edited by
            #22

            what's cool about that code though? It's directly translatable from visual graphs of the state objects it represents. I use graphviz to visualize them, and you can see right in the graph where the code matches up. It's beautiful - a 1:1 translation. So the code actually is human readable, you just need the map (the visual graph) for it.

            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

            1 Reply Last reply
            0
            • H honey the codewitch

              i mean, i agree that it shouldn't, but it causes control flow changes and can be used that way. but like i said, i agree. Just because you can do something, doesn't mean you should.

              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #23

              I've seen MS themselves doing it. Sometimes when external code that you don't have control over lacks certain functionality, it might be your only choice. (been there, done that. X| )

              Wrong is evil and must be defeated. - Jeff Ello

              H 1 Reply Last reply
              0
              • J Jorgen Andersson

                I've seen MS themselves doing it. Sometimes when external code that you don't have control over lacks certain functionality, it might be your only choice. (been there, done that. X| )

                Wrong is evil and must be defeated. - Jeff Ello

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #24

                right? that's why i *never* throw! kidding! but in seriousness I take great pains to prevent the users of my code from having to catch exceptions on failure if they don't want them. Like I'll provide TryXXXX to complement XXXX Pck: Code Roundup and Quick Start Guide[^] In PCK I have an elaborate error handling system whereby it finds errors and continues processing only (optionally) throwing an exception at the end with ALL the errors it encountered. Otherwise they're reported as "messages" with different severity/errorlevels probably the most elaborate exception handling i've had to do in managed code.

                When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                1 Reply Last reply
                0
                • H honey the codewitch

                  why do this?

                  for(int i = 0;i
                  instead of

                  for(int i = 0;i

                  hengh?? why you still use break?

                  :laugh:

                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                  Sander RosselS Offline
                  Sander RosselS Offline
                  Sander Rossel
                  wrote on last edited by
                  #25

                  Depending on the length of the array, just let it loop :D I rarely use break or continue (break slightly more often, but probably only a handful in my entire career). I've never used goto, except to mock you I think :D I'd use FirstOrDefault here (which probably uses a break, I don't know). This kind of functionality is embedded in .NET, so why would I write my own? But if I had to pick, I'd choose the break, which far more clearly communicates intent than i=arr.Length;.

                  honey the codewitch wrote:

                  } // braces copyright Sander Rossel

                  So proud of my honeycode the witch :D

                  Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                  H 1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    Depending on the length of the array, just let it loop :D I rarely use break or continue (break slightly more often, but probably only a handful in my entire career). I've never used goto, except to mock you I think :D I'd use FirstOrDefault here (which probably uses a break, I don't know). This kind of functionality is embedded in .NET, so why would I write my own? But if I had to pick, I'd choose the break, which far more clearly communicates intent than i=arr.Length;.

                    honey the codewitch wrote:

                    } // braces copyright Sander Rossel

                    So proud of my honeycode the witch :D

                    Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                    H Offline
                    H Offline
                    honey the codewitch
                    wrote on last edited by
                    #26

                    if code is hard to write, it should be hard to read. :laugh: i'm a sucker for symmetry.

                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      why do this?

                      for(int i = 0;i
                      instead of

                      for(int i = 0;i

                      hengh?? why you still use break?

                      :laugh:

                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

                      I have even encountered this in pseudocode, where the author could easily have added a `break` to his made-up exposition-purposes-only language but chose a more obfuscated way instead.

                      1 Reply Last reply
                      0
                      • N Nand32

                        At times, the purpose of the search might be just to find the index of the valueToFind. Break keeps the index safe?

                        (JS)
                        var arr =[0,1,2,3,4,5];
                        var valueToFind = 3;

                        for(i=0;i

                        H Offline
                        H Offline
                        honey the codewitch
                        wrote on last edited by
                        #28

                        right, but in the OP i limited i to the loop scope, but yeah.

                        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                        1 Reply Last reply
                        0
                        • H honey the codewitch

                          why do this?

                          for(int i = 0;i
                          instead of

                          for(int i = 0;i

                          hengh?? why you still use break?

                          :laugh:

                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                          N Offline
                          N Offline
                          Night Fox Localhost
                          wrote on last edited by
                          #29

                          I prefer break statements. It is not suitable for big projects it might be confusing. I try my level best to write clean code!

                          1 Reply Last reply
                          0
                          • H honey the codewitch

                            why do this?

                            for(int i = 0;i
                            instead of

                            for(int i = 0;i

                            hengh?? why you still use break?

                            :laugh:

                            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #30

                            Because the second fragment duplicates knowledge of the loop's termination condition. You have to remember to adjust it in two places. The first fragment is therefore more robust.

                            Software Zen: delete this;

                            H 1 Reply Last reply
                            0
                            • G Gary R Wheeler

                              Because the second fragment duplicates knowledge of the loop's termination condition. You have to remember to adjust it in two places. The first fragment is therefore more robust.

                              Software Zen: delete this;

                              H Offline
                              H Offline
                              honey the codewitch
                              wrote on last edited by
                              #31

                              yep. although there are cases where I'll modify i inside the loop for other reasons. Like if I have to add or remove items while enumerating (it happens with complicated algos)

                              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                              G 1 Reply Last reply
                              0
                              • H honey the codewitch

                                why do this?

                                for(int i = 0;i
                                instead of

                                for(int i = 0;i

                                hengh?? why you still use break?

                                :laugh:

                                When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

                                Yours is BS because it will execute everything after the if.

                                H 1 Reply Last reply
                                0
                                • L Lost User

                                  Yours is BS because it will execute everything after the if.

                                  H Offline
                                  H Offline
                                  honey the codewitch
                                  wrote on last edited by
                                  #33

                                  It was sample code. Normally you'd account for that when you put actual code in there, but without putting anything in there, it's not for me to know where the actual break should happen. However, it was sort of written with the idea that it would follow the main loop logic, kind of like the break example would. Of course the control flow is slightly different, but it's not irreconcilably different. if you need something to go after the conditional check, you'd wrap whatever went after the break in the one example in an else block instead.

                                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                  L 1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    It was sample code. Normally you'd account for that when you put actual code in there, but without putting anything in there, it's not for me to know where the actual break should happen. However, it was sort of written with the idea that it would follow the main loop logic, kind of like the break example would. Of course the control flow is slightly different, but it's not irreconcilably different. if you need something to go after the conditional check, you'd wrap whatever went after the break in the one example in an else block instead.

                                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

                                    Yeas it the simpliest possible 5 lines code. In real life you would probably have another couple of hundreds lines of similar mess entagled there, with some poor soul wondering why is this piece of crap executing when it was not supposed to.

                                    H 1 Reply Last reply
                                    0
                                    • L Lost User

                                      Yeas it the simpliest possible 5 lines code. In real life you would probably have another couple of hundreds lines of similar mess entagled there, with some poor soul wondering why is this piece of crap executing when it was not supposed to.

                                      H Offline
                                      H Offline
                                      honey the codewitch
                                      wrote on last edited by
                                      #35

                                      :laugh: it's funny cuz it's true. For the record, I'm not endorsing the method i described, I'm simply being facetious about it. I think it's silly. A break statement is much clearer, which was kind of the point of my OP. Sometimes you need break. Or a continue. Or even a goto (which i can give a solid use case for - in this case making the code MORE readable)

                                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                      L 1 Reply Last reply
                                      0
                                      • H honey the codewitch

                                        :laugh: it's funny cuz it's true. For the record, I'm not endorsing the method i described, I'm simply being facetious about it. I think it's silly. A break statement is much clearer, which was kind of the point of my OP. Sometimes you need break. Or a continue. Or even a goto (which i can give a solid use case for - in this case making the code MORE readable)

                                        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

                                        Alright, here a piece of advice: never exit conditionally a for cycle. if you need to do that, use while or do until.

                                        H 1 Reply Last reply
                                        0
                                        • L Lost User

                                          Alright, here a piece of advice: never exit conditionally a for cycle. if you need to do that, use while or do until.

                                          H Offline
                                          H Offline
                                          honey the codewitch
                                          wrote on last edited by
                                          #37

                                          i think it depends on the situation for example

                                          public int IndexOf(T item)
                                          {
                                          var ic = Count;
                                          var i = 0;
                                          for(;i

                                          That's very clear even without comments

                                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                          L 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