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. Today, I used goto to double-break out of nested loops

Today, I used goto to double-break out of nested loops

Scheduled Pinned Locked Moved The Lounge
csharpc++phpcomquestion
35 Posts 26 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.
  • N Nemanja Trifunovic

    I guess I'd move the nested loops into a separate function and called return instead, but that's really goto in disguise anyway :)

    utf8-cpp

    T Offline
    T Offline
    Tom Chantler
    wrote on last edited by
    #23

    That's what I'd have done too. You're allowed to do stuff like that! :)

    1 Reply Last reply
    0
    • P peterchen

      "and I feel fine!" (to say it with R.E.M.)

      Agh! Reality! My Archnemesis![^]
      | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

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

      So you're the one responsible the north korean attacks today... Shame on you!

      1 Reply Last reply
      0
      • P PIEBALDconsult

        Mike Hankey wrote:

        reduced to writing VB

        Oh shiioot... is that what happened? But I swear it was in a switch! :sigh:

        M Offline
        M Offline
        Mike Hankey
        wrote on last edited by
        #25

        PIEBALDconsult wrote:

        Oh shiioot... is that what happened? But I swear it was in a switch!

        It's like wearing a pocket protector, once your caught wearing it your labeled and there's no turning back.

        Artificial Intelligence is no match for Natural Stupidity. http://www.hq4thmarinescomm.com[^] My Site

        1 Reply Last reply
        0
        • P peterchen

          "and I feel fine!" (to say it with R.E.M.)

          Agh! Reality! My Archnemesis![^]
          | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

          R Offline
          R Offline
          R Erasmus
          wrote on last edited by
          #26

          If I had to test your software I would of raized a bug unfortunately. Its against standard. Not following a standard costs money. Might get away with this at your current job but will cost the next company alot of money... E.g. software tagged rev 1.0 unit test fix bugs... goto, 300 occurences. 300 occurences change source code, 300 source files software tagged rev 2.0 unit test 300 source files raize more bugs that was made on fixing "removal of goto's" fix bugs software tagged rev 3.0 unit test outstanding bugs deliver software amounts to $500 000, you just cost the company. have to retrench a few.:~ P.S. !please vote! A computer programmer is someone who, when told to "Go to Hell", sees the "Go to", rather than the destination, as harmful."

          "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

          P 1 Reply Last reply
          0
          • R R Erasmus

            If I had to test your software I would of raized a bug unfortunately. Its against standard. Not following a standard costs money. Might get away with this at your current job but will cost the next company alot of money... E.g. software tagged rev 1.0 unit test fix bugs... goto, 300 occurences. 300 occurences change source code, 300 source files software tagged rev 2.0 unit test 300 source files raize more bugs that was made on fixing "removal of goto's" fix bugs software tagged rev 3.0 unit test outstanding bugs deliver software amounts to $500 000, you just cost the company. have to retrench a few.:~ P.S. !please vote! A computer programmer is someone who, when told to "Go to Hell", sees the "Go to", rather than the destination, as harmful."

            "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #27

            RudolfErasmus wrote:

            P.S. !please vote!

            OK, I voted a 3 ;) If the coding standard was "goto is prohibited", I'd follow that. Here, the disgust of goto is big enough that we have about a dozen in over a million LOC (hundreds in 3rd party code, though...) Regarding goto in general: it's a "jump forward", wich does not increase complexity. The typical solutions would be: (a) isolate the inner loop and "hide" the goto behind a early return (if permitted by standards, but requiring single exit makes no sense in C++ anymore). (b) introduce a flag that "bumps" a second break. Now, the first solution is ok to comply with a coding standard, but is complicated if the two loops need a lot of context from the calling function. Even if not, the code compexity is increased, the function needs a contract and additional error chhecking. The second solution increases complexity as well, and makes the code harder to reason about.

            Agh! Reality! My Archnemesis![^]
            | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

            R R 2 Replies Last reply
            0
            • P peterchen

              RudolfErasmus wrote:

              P.S. !please vote!

              OK, I voted a 3 ;) If the coding standard was "goto is prohibited", I'd follow that. Here, the disgust of goto is big enough that we have about a dozen in over a million LOC (hundreds in 3rd party code, though...) Regarding goto in general: it's a "jump forward", wich does not increase complexity. The typical solutions would be: (a) isolate the inner loop and "hide" the goto behind a early return (if permitted by standards, but requiring single exit makes no sense in C++ anymore). (b) introduce a flag that "bumps" a second break. Now, the first solution is ok to comply with a coding standard, but is complicated if the two loops need a lot of context from the calling function. Even if not, the code compexity is increased, the function needs a contract and additional error chhecking. The second solution increases complexity as well, and makes the code harder to reason about.

              Agh! Reality! My Archnemesis![^]
              | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

              R Offline
              R Offline
              R Erasmus
              wrote on last edited by
              #28

              Yeah it depends what you're coding standard says. I do software testing in c(structured programming). Statement such as goto and continue is unstructured. Breaking the code up into smaller functions can help with complexity (if you care about the complexity rating, otherwise complexity in general).:thumbsup:

              "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

              1 Reply Last reply
              0
              • P peterchen

                "and I feel fine!" (to say it with R.E.M.)

                Agh! Reality! My Archnemesis![^]
                | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

                A Offline
                A Offline
                Alan Balkany
                wrote on last edited by
                #29

                Let's not forget that one of the goals of well-crafted code is clarity. If a goto helps you achieve more clarity, it's more correct than dogmatically following heuristics as if they were commandments. On the other hand, if your code requires gotos for clarity, it's a sign that there may be other problems, such as methods that are too long. Refactoring (as someone else already suggested) is probably the next step that should be considered in this situation.

                P 1 Reply Last reply
                0
                • P peterchen

                  "and I feel fine!" (to say it with R.E.M.)

                  Agh! Reality! My Archnemesis![^]
                  | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

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

                  OMG!!! I can see the coming of the apocalypse !!

                  1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    I guess I'd move the nested loops into a separate function and called return instead, but that's really goto in disguise anyway :)

                    utf8-cpp

                    D Offline
                    D Offline
                    DarthDana
                    wrote on last edited by
                    #31

                    Not really. A GOTO doesn't involve a push/pull stack operation so you have to be real careful if you are "gotoing" out of a function or subroutine. You can get away with jumping out of a function only so many times before you get a stack overflow error. Could also cause issues when the program terminates if there isn't very good garbage cleanup. Can you say "memory leak"? You're right about stuffing them into a function call and then using a return to get out, though. That would preserve stack integrity.

                    1 Reply Last reply
                    0
                    • J Jim Crafton

                      You're a dirty little pervert! Somewhere Edsger Dijkstra's is choking on a Raise Error statement!

                      ¡El diablo está en mis pantalones! ¡Mire, mire! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! Personal 3D projects Just Say No to Web 2 Point Blow

                      B Offline
                      B Offline
                      Bill Gord
                      wrote on last edited by
                      #32

                      The one time I met the respectable Prof E.W. Dijkstra (1977 perhaps), he told me that my wife's piano was out of tune. It was of course, but the remark was both rude and irrelevant. I haven't totally trusted him since then. I have no great love for the absolute adherence to Politically Correct (PC, huummm) programming practices either. If your double-break makes sense and works, then good for you !!

                      1 Reply Last reply
                      0
                      • P peterchen

                        RudolfErasmus wrote:

                        P.S. !please vote!

                        OK, I voted a 3 ;) If the coding standard was "goto is prohibited", I'd follow that. Here, the disgust of goto is big enough that we have about a dozen in over a million LOC (hundreds in 3rd party code, though...) Regarding goto in general: it's a "jump forward", wich does not increase complexity. The typical solutions would be: (a) isolate the inner loop and "hide" the goto behind a early return (if permitted by standards, but requiring single exit makes no sense in C++ anymore). (b) introduce a flag that "bumps" a second break. Now, the first solution is ok to comply with a coding standard, but is complicated if the two loops need a lot of context from the calling function. Even if not, the code compexity is increased, the function needs a contract and additional error chhecking. The second solution increases complexity as well, and makes the code harder to reason about.

                        Agh! Reality! My Archnemesis![^]
                        | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

                        R Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #33

                        The D Language has an excellent feature to avoid this problem. Labelled break:

                        outer:
                        while (cond1) {
                        // ...
                        inner:
                        while (cond2) {
                        // ...
                        if (cond3)
                        break inner;

                                if (cond4)
                                    break outer;
                                // ...
                            }
                            // ...
                        }
                        

                        This seems elegant to me - of course D also provides a goto, I find it hard to see a reason for that once labelled breaks are allowed - they seem the one fringe case that excuses use of goto's.

                        1 Reply Last reply
                        0
                        • P peterchen

                          "and I feel fine!" (to say it with R.E.M.)

                          Agh! Reality! My Archnemesis![^]
                          | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

                          M Offline
                          M Offline
                          Member 2053006
                          wrote on last edited by
                          #34

                          I hope you commented that with: Do not try this at home, script-kiddies. ;-)

                          1 Reply Last reply
                          0
                          • A Alan Balkany

                            Let's not forget that one of the goals of well-crafted code is clarity. If a goto helps you achieve more clarity, it's more correct than dogmatically following heuristics as if they were commandments. On the other hand, if your code requires gotos for clarity, it's a sign that there may be other problems, such as methods that are too long. Refactoring (as someone else already suggested) is probably the next step that should be considered in this situation.

                            P Offline
                            P Offline
                            pg az
                            wrote on last edited by
                            #35

                            Alan Balkany wrote:

                            If a goto helps you achieve more clarity, it's more correct than dogmatically following heuristics as if they were commandments

                            That is well spoken. When it happens that more than one branch of a switch Naturally-Shares-Stuff-At-The-Bottom, it has always seemed Clearer to me, to goto L_shared_stuff_for_these_branches. NAMED LABELS are so wonderfully clear, compared to a nameless-break, where you MUST visually scan the close-braces to make your best guess as to where it goes. On 22-nov at 08:52 OriginalGriff remarked that he also was not a believer in the "single exit" - Actually I HAVE come to be a TRUE BELIEVER in the SINGLE-EXIT, unless you can think of some easy way to make the OTHER exits easily SPOTTED. With multiple exits, you need to scan the ENTIRE routine to make SURE you have spotted all the exits. This is in stark contrast to the goto L_shared_stuff_for_these_branches, which actually REDUCES the visual scanning, you can just select the label and do control-F3 to see where it ends up. On 24-nov at 4:00 Rob Grainger remarked on "Labelled break" in the D-Language, which avoids the goto but still does not greatly reduce your visual scanning. To summarize, debate-wise you need to stick the Camel's nose into the tent by finding some classic idiom which is manifestly MORE clear with the goto. None of my code is "classic", so there's work to do there. Having proven that there is at least one case where goto is obviously beneficial, then your "dogma" label really sticks. Because something CAN BE harmful, to ban it, thus eliminating its beneficial uses - why we have reinvented the "Nanny State" !!!

                            pg--az

                            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