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

    It is simpler and I actually use pretty much exactly the same code when it is that simple. When you have more than a page of lines within that loop a sudden exit is not what I would do. And a sudden conditional exit with break or return might be even ok. Putting the i to the max and then exit in that manner is something I would never do.

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

    well to each their own. probably it also has to do with my C++ background which influences a lot of my code. Not that C++ has different flow constructs, it's just that a) i learned these habits a long time ago and the industry changes b) C++ development is a different animal, and control flow is all over the place generally and for loops are used for almost everything except while(true) although some people go for(;;). It's not uncommon even to do stuff like for(current=firstNode;null!=current;current=current.nextNode) if(current->key==key) break; to traverse a linked list for example.

    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

      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 Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #42

      That makes sense, especially given the type of algorithms you deal with in parsers and data structures. In the course of developing several large, complex applications, I've learned that having pieces of code that must stay in sync logically or follow the same algorithm is a failure point. Refactoring can help if i makes sense to move things into a method, and then have each location invoke the method. The hard part there can be figuring out a name for the thing: "CheckToSeeIfMessageNeededAtThreadExit" is ugly :-D.

      Software Zen: delete this;

      H 1 Reply Last reply
      0
      • G Gary R Wheeler

        That makes sense, especially given the type of algorithms you deal with in parsers and data structures. In the course of developing several large, complex applications, I've learned that having pieces of code that must stay in sync logically or follow the same algorithm is a failure point. Refactoring can help if i makes sense to move things into a method, and then have each location invoke the method. The hard part there can be figuring out a name for the thing: "CheckToSeeIfMessageNeededAtThreadExit" is ugly :-D.

        Software Zen: delete this;

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

        right. Extract Method is one of my favorite refactoring tools I don't use incredibly long names for private methods. I'll abbreviate something like the above to _CheckMessageThread() Public members i usually go all out, and give it a really long name if it needs one.

        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.

          S Offline
          S Offline
          SwitcherSoft
          wrote on last edited by
          #44

          A purist should be worried about touching control variables. Hmmm.

          Jordan

          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.

            J Offline
            J Offline
            John Brett
            wrote on last edited by
            #45

            Quote:

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

            Isn't this exactly the point? All code gets compiled/interpreted/translated to jmps eventually. The goals of the written code should be correctness, understandability and simplicity. Leave the gotos and the clever techniques to the compiler.

            A H 2 Replies Last reply
            0
            • L Lost User

              throw would be even more clear, definite and failure proof. (well I do see many kids using exactly that to 'not use goto endlabel')

              Message Signature (Click to edit ->)

              K Offline
              K Offline
              kalberts
              wrote on last edited by
              #46

              If finding the desired value is exceptional and unexpected and requiring special handling, then throwing an exception is appropriate. If it is the normal and desired case, exeactly what you expected: "Yeah, there it is!", then an exception is not the right mechanism.

              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.

                T Offline
                T Offline
                t j home
                wrote on last edited by
                #47

                break is (of course) superfluous - but why not try something that suggests the intention of your loop?

                for(int i = 0;i

                ... or (because I don't like "empty" constructs) ...

                int i = 0;
                while (i

                1 Reply Last reply
                0
                • J John Brett

                  Quote:

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

                  Isn't this exactly the point? All code gets compiled/interpreted/translated to jmps eventually. The goals of the written code should be correctness, understandability and simplicity. Leave the gotos and the clever techniques to the compiler.

                  A Offline
                  A Offline
                  Andrew Leeder
                  wrote on last edited by
                  #48

                  You trust your compiler?? Wow!

                  1 Reply Last reply
                  0
                  • J John Brett

                    Quote:

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

                    Isn't this exactly the point? All code gets compiled/interpreted/translated to jmps eventually. The goals of the written code should be correctness, understandability and simplicity. Leave the gotos and the clever techniques to the compiler.

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

                    I'll optimize when i need to. that doesn't always make the code 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.

                    J 1 Reply Last reply
                    0
                    • H honey the codewitch

                      I'll optimize when i need to. that doesn't always make the code 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.

                      J Offline
                      J Offline
                      John Brett
                      wrote on last edited by
                      #50

                      The reality is that with modern code, it's so far removed from the underlying machine language that gets executed that there's no point trying optimisations at the level of a break vs setting the iterator. Optimisations nowadays are at architectural levels - managing tight loops, using appropriate data structures, parallelisation, resource access.

                      H 1 Reply Last reply
                      0
                      • J John Brett

                        The reality is that with modern code, it's so far removed from the underlying machine language that gets executed that there's no point trying optimisations at the level of a break vs setting the iterator. Optimisations nowadays are at architectural levels - managing tight loops, using appropriate data structures, parallelisation, resource access.

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

                        yes and no. it depends on whether you consider algorithmic optimizations to be architecture. For example, my first crack at LALR(1) table generation was taking 5 minutes to generate the tables for javascript. My second one cut that to a 5th of the time. The cost was code that was no longer "pure" and readable. It wasn't an architecture change. Unless you think it was. But I wouldn't agree, and I wrote 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

                          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.

                          M Offline
                          M Offline
                          maze3
                          wrote on last edited by
                          #52

                          I fixed your mistake, you didn't put some space after semi-colons in the for statement.

                          Book[] arr = books;
                          for(int i = 0; i

                          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.

                            M Offline
                            M Offline
                            MSBassSinger
                            wrote on last edited by
                            #53

                            Why not? What is the net value gain by the alternative you show?

                            H 1 Reply Last reply
                            0
                            • M MSBassSinger

                              Why not? What is the net value gain by the alternative you show?

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

                              there is none

                              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.

                                O Offline
                                O Offline
                                obermd
                                wrote on last edited by
                                #55

                                Those two code snippets don't do the same thing. The first doesn't change arr, the second does.

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

                                  W Offline
                                  W Offline
                                  W Balboos GHB
                                  wrote on last edited by
                                  #56

                                  Purist? Baaah! Think of this in terms of far-Eastern philosophy, to wit, Yin/Yang[^]. Always the spot of yin in the yang portion, the spot of yang in the yin portion. Neither can exist without the other. So, continue to use break as they help you goto a better place.

                                  Ravings en masse^

                                  "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                  "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                                  H 1 Reply Last reply
                                  0
                                  • O obermd

                                    Those two code snippets don't do the same thing. The first doesn't change arr, the second does.

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

                                    How does it do that? If it does, it is a bug

                                    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.

                                      S Offline
                                      S Offline
                                      SeattleC
                                      wrote on last edited by
                                      #58

                                      Because neither one of these loops does anything except waste time. Was there perhaps a reason you wanted to find out if valueToFind was in arr? If there was, the first loop is almost right, i points to the matching entry on loop exit. Only problem is, i goes out of scope on loop exit. Sigh. The second loop always has i== arr.Length on loop exit, and i still goes out of scope

                                      H 1 Reply Last reply
                                      0
                                      • S SeattleC

                                        Because neither one of these loops does anything except waste time. Was there perhaps a reason you wanted to find out if valueToFind was in arr? If there was, the first loop is almost right, i points to the matching entry on loop exit. Only problem is, i goes out of scope on loop exit. Sigh. The second loop always has i== arr.Length on loop exit, and i still goes out of scope

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

                                        the code to do something is supposed to go in the loop body. i omitted it for the example. sorry i wasn't more clear.

                                        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
                                        • W W Balboos GHB

                                          Purist? Baaah! Think of this in terms of far-Eastern philosophy, to wit, Yin/Yang[^]. Always the spot of yin in the yang portion, the spot of yang in the yin portion. Neither can exist without the other. So, continue to use break as they help you goto a better place.

                                          Ravings en masse^

                                          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                                          "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

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

                                          Prince Wang's programmer was coding software. His fingers danced upon the keyboard. The program compiled without and error message, and the program ran like a gentle wind. "Excellent!" the Prince exclaimed. "Your technique is faultless!" "Technique?" said the programmer, turning from his terminal, "What I follow is Tao -- beyond all techniques! When I first began to program, I would see before me the whole problem in one mass. After three years, I no longer saw this mass. Instead, I used subroutines. But now I see nothing. My whole being exists in a formless void. My senses are idle. My spirit, free to work without a plan, follows its own instinct. In short, my program writes itself. True, sometimes there are difficult problems. I see them coming, I slow down, I watch silently. Then I change a single line of code and the difficulties vanish like puffs of idle smoke. I then compile the program. I sit still and let the joy of the work fill my being. I close my eyes for a moment and then log off." Prince Wang said, "Would that all of my programmers were as wise!" - The Tao of Programming

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