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. You'll be glad to know! [modified]

You'll be glad to know! [modified]

Scheduled Pinned Locked Moved The Lounge
csharpcsscomalgorithmsperformance
73 Posts 17 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.
  • S Super Lloyd

    Glad to see that not everyone give in the anti goto dogma, an actually look at the code! :-D And yeah, I have been unconvinced by all the gymnastic to get rid of the goto so far. Seems much easier, shorter, etc... with it! I'm not really sure why I made this post in fact.. but I think I learn something about communication! :)

    A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

    Steve EcholsS Offline
    Steve EcholsS Offline
    Steve Echols
    wrote on last edited by
    #48

    Right on. If you start using nested gotos, or didn't label them nicely, I'd have to hire JSOP to come after you. :-D Remember, it's all good code, until you have to maintain it. Double :-D


    - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

    • S
      50 cups of coffee and you know it's on!
      Code, follow, or get out of the way.
    1 Reply Last reply
    0
    • P Patrick Klug

      If you don't like brackets, refactor the nested loop into another method. The way I see it you just WANT to use goto's even though you know that 'most other people' (TM) would prefer the equally valid version without goto's. Personally it upsets me that people use language features for no good reason. 'I don't like brackets' is not a good reason at all. Code with a goto is always harder to decipher than code without.

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #49

      Basically you don't want goto, why do I need to do as you like? No compelling reason so far!

      A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

      1 Reply Last reply
      0
      • P Patrick Klug

        If you don't like brackets, refactor the nested loop into another method. The way I see it you just WANT to use goto's even though you know that 'most other people' (TM) would prefer the equally valid version without goto's. Personally it upsets me that people use language features for no good reason. 'I don't like brackets' is not a good reason at all. Code with a goto is always harder to decipher than code without.

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #50

        In fact, I noticed you gave an argument: "is harder to decipher" It's a good argument, it just happen I find the goto version more readable! There is nothing much to say...

        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

        1 Reply Last reply
        0
        • S Super Lloyd

          Yesterday I wrote no less than 2 gotos in my C# code! [Edit2] This guy[^] found a satisfying refactoring! One which doesn't tax my (limited brain) memory with nested block, additional variable, additional test, increased cyclomatic complexity! (In fact the cyclomatic complexity is reduced by 1, I think) Well done! :thumbsup: :cool: :-D [EDIT] For your information the real code is below. So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less) -- code --

          for (int i = max; i >= min; i--)
          {
          var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
          var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
          if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
          goto RemovePoint;
          v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
          v2 = v2.Normalize();
          var z = v1 ^ v2;
          if (Math.Abs(z) <= minsin && v1 * v2 < 0)
          goto RemovePoint;
          continue;
          RemovePoint: ;
          points.RemoveAt(i);
          if (points.Count < 3)
          return null;
          }

          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

          modified on Friday, September 10, 2010 10:53 AM

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

          Super Lloyd wrote:

          So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less)

          Um, setting arbitrary rules to suit your purpose only shows that you've used goto for arbitrary purposes. Cheers, Drew.

          S 1 Reply Last reply
          0
          • L Lost User

            Super Lloyd wrote:

            So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less)

            Um, setting arbitrary rules to suit your purpose only shows that you've used goto for arbitrary purposes. Cheers, Drew.

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #52

            mm.. I though i a good idea because people were showing other code which they preferred but I found more confusing. So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!

            A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

            L 1 Reply Last reply
            0
            • Steve EcholsS Steve Echols

              I'm not as much of a hater of goto's as most people, but without goto's I'd write it as:

              for (int i = max; i >= min; i--)
              {
              bool removePoint = false;
              var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
              var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
              if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
              {
              removePoint = true;
              }
              else
              {
              v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
              v2 = v2.Normalize();
              var z = v1 ^ v2;
              if (Math.Abs(z) <= minsin && v1 * v2 < 0)
              removePoint = true;
              }

                  if (removePoint)
                  {
                      points.RemoveAt(i);
                      if (points.Count < 3)
                          return null;
                  }
              

              }

              Either way, it's pretty clear. Yours doesn't require the extra allocation/initialization/assignment of the flag, so it's slightly more efficient, which could be important if you're in a huge loop.


              - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

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

              Now just get rid of that mid-loop return and we'll all be happy! (any arbitrary break in flow is the same as a goto in my book). Cheers, Drew.

              Steve EcholsS 1 Reply Last reply
              0
              • L Lost User

                Now just get rid of that mid-loop return and we'll all be happy! (any arbitrary break in flow is the same as a goto in my book). Cheers, Drew.

                Steve EcholsS Offline
                Steve EcholsS Offline
                Steve Echols
                wrote on last edited by
                #54

                Yeah, but kinda depends on what's after the loop though. Could replace with a break; but then you might end up having to do another if (points.Count < 3) Sometimes getting the hell out of dodge is the best option. Depends, it always just depends. As long as it's clear what's going on, right? 1001 ways to skin a cat (although I would never do that, unless I was beyond hunger, or it was a Kobe cat!) :)


                - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

                • S
                  50 cups of coffee and you know it's on!
                  Code, follow, or get out of the way.
                L 1 Reply Last reply
                0
                • S Super Lloyd

                  mm.. I though i a good idea because people were showing other code which they preferred but I found more confusing. So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!

                  A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

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

                  Super Lloyd wrote:

                  So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!

                  Of course you code to a standard that you prefer. Everyone does. What others are trying to explain to you is that there are other ways that, once you get used to them, lead to a lot fewer problems with bugs and software maintenance. Structure in your code is one of those ways; gotos and returns from within loops are two things that dimish the structure of your program and make it very hard to understand by people who didn't write the code in the first place. Adding conditionals with well named variables is an easy way to get rid of gotos and gives you additional contextual clues that help you and others understand what your program is doing and why, even if you're looking at it years from now. Cheers, Drew.

                  S 1 Reply Last reply
                  0
                  • L Lost User

                    Super Lloyd wrote:

                    So I clearly expressed why I prefer my code, so that those who want to help me get free of the shackle of the evil goto, can do so in a productive way instead of wasting both our time!

                    Of course you code to a standard that you prefer. Everyone does. What others are trying to explain to you is that there are other ways that, once you get used to them, lead to a lot fewer problems with bugs and software maintenance. Structure in your code is one of those ways; gotos and returns from within loops are two things that dimish the structure of your program and make it very hard to understand by people who didn't write the code in the first place. Adding conditionals with well named variables is an easy way to get rid of gotos and gives you additional contextual clues that help you and others understand what your program is doing and why, even if you're looking at it years from now. Cheers, Drew.

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #56

                    My reply is that I should not have reply to all this post, this is going nowhere! Particularly all those alternative are nothing new, so it's mostly boring... Just for the record I write an average of 1 goto a year. I'm happy with it and last I look back at 10 years old code with goto, I still find it much easier to understand this way!

                    A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                    L 1 Reply Last reply
                    0
                    • Steve EcholsS Steve Echols

                      Yeah, but kinda depends on what's after the loop though. Could replace with a break; but then you might end up having to do another if (points.Count < 3) Sometimes getting the hell out of dodge is the best option. Depends, it always just depends. As long as it's clear what's going on, right? 1001 ways to skin a cat (although I would never do that, unless I was beyond hunger, or it was a Kobe cat!) :)


                      - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

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

                      Steve Echols wrote:

                      Could replace with a break;

                      Ug, I hate 'breaks' too. I guess I've never seen a good reason to break the flow of code with any of those things. I'd just add another variable and throw it in with the conditional in the for loop and then check it again after the loop if necessary (like you say - depends on what else is going on). Adds more code, but the conditional variable at least makes it obvious why the loop is being aborted, and the check for it goes with the loop construct where it should be anyway (all the reasons for stopping the loop are in one place).

                      var result=null;
                      bool enoughPoints=true;
                      for (int i = max; i >= min && enoughPoints; i--)
                      {
                      bool removePoint = false;
                      var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
                      var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
                      if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
                      {
                      removePoint = true;
                      }
                      else
                      {
                      v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
                      v2 = v2.Normalize();
                      var z = v1 ^ v2;
                      if (Math.Abs(z) <= minsin && v1 * v2 < 0)
                      {
                      removePoint = true;
                      }
                      }
                      if (removePoint)
                      {
                      points.RemoveAt(i);
                      if (points.Count < 3)
                      {
                      enoughPoints=false;
                      }
                      }
                      }
                      if(enoughPoints)
                      {
                      ...some other code that may affect 'result'
                      }
                      return result;

                      Cheers, Drew.

                      Steve EcholsS 1 Reply Last reply
                      0
                      • S Super Lloyd

                        My reply is that I should not have reply to all this post, this is going nowhere! Particularly all those alternative are nothing new, so it's mostly boring... Just for the record I write an average of 1 goto a year. I'm happy with it and last I look back at 10 years old code with goto, I still find it much easier to understand this way!

                        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

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

                        Super Lloyd wrote:

                        My reply is that I should not have reply to all this post, this is going nowhere!

                        Heh, then why are you replying? Cheers, Drew.

                        S 1 Reply Last reply
                        0
                        • L Lost User

                          Super Lloyd wrote:

                          My reply is that I should not have reply to all this post, this is going nowhere!

                          Heh, then why are you replying? Cheers, Drew.

                          S Offline
                          S Offline
                          Super Lloyd
                          wrote on last edited by
                          #59

                          Oops, I mean "my problem"! But... you're right! :laugh: (I'm still learning) .....

                          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                          1 Reply Last reply
                          0
                          • L Lost User

                            Steve Echols wrote:

                            Could replace with a break;

                            Ug, I hate 'breaks' too. I guess I've never seen a good reason to break the flow of code with any of those things. I'd just add another variable and throw it in with the conditional in the for loop and then check it again after the loop if necessary (like you say - depends on what else is going on). Adds more code, but the conditional variable at least makes it obvious why the loop is being aborted, and the check for it goes with the loop construct where it should be anyway (all the reasons for stopping the loop are in one place).

                            var result=null;
                            bool enoughPoints=true;
                            for (int i = max; i >= min && enoughPoints; i--)
                            {
                            bool removePoint = false;
                            var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
                            var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
                            if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
                            {
                            removePoint = true;
                            }
                            else
                            {
                            v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
                            v2 = v2.Normalize();
                            var z = v1 ^ v2;
                            if (Math.Abs(z) <= minsin && v1 * v2 < 0)
                            {
                            removePoint = true;
                            }
                            }
                            if (removePoint)
                            {
                            points.RemoveAt(i);
                            if (points.Count < 3)
                            {
                            enoughPoints=false;
                            }
                            }
                            }
                            if(enoughPoints)
                            {
                            ...some other code that may affect 'result'
                            }
                            return result;

                            Cheers, Drew.

                            Steve EcholsS Offline
                            Steve EcholsS Offline
                            Steve Echols
                            wrote on last edited by
                            #60

                            Depends :) As long as there's nothing after the if (removePoints) block that relies on having enough points, that works for me! Just gotta structure everything right, to minimize checks. Dang depends! Starting to feel like I'm about to have a coding accident now! :-D


                            - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

                            • S
                              50 cups of coffee and you know it's on!
                              Code, follow, or get out of the way.
                            L 1 Reply Last reply
                            0
                            • Steve EcholsS Steve Echols

                              Depends :) As long as there's nothing after the if (removePoints) block that relies on having enough points, that works for me! Just gotta structure everything right, to minimize checks. Dang depends! Starting to feel like I'm about to have a coding accident now! :-D


                              - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

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

                              Heh, I was thinking the same thing! Cheers, Drew.

                              Steve EcholsS 1 Reply Last reply
                              0
                              • L Lost User

                                Heh, I was thinking the same thing! Cheers, Drew.

                                Steve EcholsS Offline
                                Steve EcholsS Offline
                                Steve Echols
                                wrote on last edited by
                                #62

                                :thumbsup: Great minds think alike!


                                - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

                                • S
                                  50 cups of coffee and you know it's on!
                                  Code, follow, or get out of the way.
                                1 Reply Last reply
                                0
                                • S Super Lloyd

                                  Yesterday I wrote no less than 2 gotos in my C# code! [Edit2] This guy[^] found a satisfying refactoring! One which doesn't tax my (limited brain) memory with nested block, additional variable, additional test, increased cyclomatic complexity! (In fact the cyclomatic complexity is reduced by 1, I think) Well done! :thumbsup: :cool: :-D [EDIT] For your information the real code is below. So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less) -- code --

                                  for (int i = max; i >= min; i--)
                                  {
                                  var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
                                  var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
                                  if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
                                  goto RemovePoint;
                                  v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
                                  v2 = v2.Normalize();
                                  var z = v1 ^ v2;
                                  if (Math.Abs(z) <= minsin && v1 * v2 < 0)
                                  goto RemovePoint;
                                  continue;
                                  RemovePoint: ;
                                  points.RemoveAt(i);
                                  if (points.Count < 3)
                                  return null;
                                  }

                                  A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                                  modified on Friday, September 10, 2010 10:53 AM

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

                                  Fast to code, easy to understand (despite the fact that the code is disastrously lacking in comments), and better performance than most alternatives. Why on Earth wouldn't you use a goto for that? The "approved" Java equivalent would create at least one factory to construct at least two additional objects to do the same thing (and no-one would be able to figure out what was going on without clicking through to the various constructors and back half a dozen times). Too many developers are D&D or LoR fans -- The Lord High Muck-a-Muck Wizard says Useth Not The Goto!, and his word must be obeyed. I'm still waiting for someone to explain to me why code that isn't broken should be fixed.

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

                                  S A 2 Replies Last reply
                                  0
                                  • M Mark_Wallace

                                    Fast to code, easy to understand (despite the fact that the code is disastrously lacking in comments), and better performance than most alternatives. Why on Earth wouldn't you use a goto for that? The "approved" Java equivalent would create at least one factory to construct at least two additional objects to do the same thing (and no-one would be able to figure out what was going on without clicking through to the various constructors and back half a dozen times). Too many developers are D&D or LoR fans -- The Lord High Muck-a-Muck Wizard says Useth Not The Goto!, and his word must be obeyed. I'm still waiting for someone to explain to me why code that isn't broken should be fixed.

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

                                    S Offline
                                    S Offline
                                    Super Lloyd
                                    wrote on last edited by
                                    #64

                                    Exactly and well said! :-D

                                    A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                                    1 Reply Last reply
                                    0
                                    • M Mark_Wallace

                                      Fast to code, easy to understand (despite the fact that the code is disastrously lacking in comments), and better performance than most alternatives. Why on Earth wouldn't you use a goto for that? The "approved" Java equivalent would create at least one factory to construct at least two additional objects to do the same thing (and no-one would be able to figure out what was going on without clicking through to the various constructors and back half a dozen times). Too many developers are D&D or LoR fans -- The Lord High Muck-a-Muck Wizard says Useth Not The Goto!, and his word must be obeyed. I'm still waiting for someone to explain to me why code that isn't broken should be fixed.

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

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

                                      GOTOs complicate code. They make what is clearly understandable structure into a possible back and forth jumble. They essentially turn a tree into a graph, which is much more difficult to navigate. With a GOTO, you can jump to anywhere in the program that has a label (in C#, that might be limited to the current method, but I'm not sure), which makes it hard to follow. With a clear code flow (conditional blocks, looping blocks, and so on), things are easier to follow. GOTOs can be useful in certain cases where performance is of extreme importance, but then again so can assembly instructions. The purpose of using a high-level language is to escape the complexity that comes with maintaining low-level constructs such as GOTO. Like I said, GOTOs can be useful to improve performance in very limited scenarios, but that doesn't mean they should be used when an alternative exists that doesn't cause strange code flow.

                                      [Forum Guidelines]

                                      M 1 Reply Last reply
                                      0
                                      • A AspDotNetDev

                                        GOTOs complicate code. They make what is clearly understandable structure into a possible back and forth jumble. They essentially turn a tree into a graph, which is much more difficult to navigate. With a GOTO, you can jump to anywhere in the program that has a label (in C#, that might be limited to the current method, but I'm not sure), which makes it hard to follow. With a clear code flow (conditional blocks, looping blocks, and so on), things are easier to follow. GOTOs can be useful in certain cases where performance is of extreme importance, but then again so can assembly instructions. The purpose of using a high-level language is to escape the complexity that comes with maintaining low-level constructs such as GOTO. Like I said, GOTOs can be useful to improve performance in very limited scenarios, but that doesn't mean they should be used when an alternative exists that doesn't cause strange code flow.

                                        [Forum Guidelines]

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

                                        So you're saying that gotos, like anything else, can be used badly. That doesn't mean that they have to be (and nor does anything else). If we exclude everything from a language because it can be used badly, there won't be a Hell of a lot left.

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

                                        1 Reply Last reply
                                        0
                                        • S Super Lloyd

                                          Yesterday I wrote no less than 2 gotos in my C# code! [Edit2] This guy[^] found a satisfying refactoring! One which doesn't tax my (limited brain) memory with nested block, additional variable, additional test, increased cyclomatic complexity! (In fact the cyclomatic complexity is reduced by 1, I think) Well done! :thumbsup: :cool: :-D [EDIT] For your information the real code is below. So far no one has come up with a non goto version which can beat the goto version on any of those 4 criteria: - number of line of code (less) - number of if (less) - number of nested block (less) - number of variables (less) -- code --

                                          for (int i = max; i >= min; i--)
                                          {
                                          var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
                                          var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
                                          if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
                                          goto RemovePoint;
                                          v1 = v1.Normalize(); // divide by zero if square norm is 0 (test above)
                                          v2 = v2.Normalize();
                                          var z = v1 ^ v2;
                                          if (Math.Abs(z) <= minsin && v1 * v2 < 0)
                                          goto RemovePoint;
                                          continue;
                                          RemovePoint: ;
                                          points.RemoveAt(i);
                                          if (points.Count < 3)
                                          return null;
                                          }

                                          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                                          modified on Friday, September 10, 2010 10:53 AM

                                          J Offline
                                          J Offline
                                          jbarton
                                          wrote on last edited by
                                          #67

                                          I wouldn't be trying to beat those criteria as they don't match the style that I prefer. I would instead refactor this to use a helper routine:

                                          bool ShouldPointBeRemoved(List points, int i)
                                          {
                                          var v1 = new Vector2D(points[i > 0 ? i - 1 : points.Count - 1], points[i]);
                                          var v2 = new Vector2D(points[i], points[i < points.Count - 1 ? i + 1 : 0]);
                                          if (v1.SquareNorm <= MINL || v2.SquareNorm <= MINL)
                                          return true;

                                            v1 = v1.Normalize();
                                            v2 = v2.Normalize();
                                            var z = v1 ^ v2;
                                            return (Math.Abs(z) <= minsin && v1 \* v2 < 0);
                                          

                                          }

                                          Note: I used a List for the points, as you never specified a type. Once this helper routine is provided, the loop becomes:

                                          for (int i = max; i >= min; i--)
                                          {
                                          if (ShouldPointBeRemoved(points, i))
                                          {
                                          points.RemoveAt(i);
                                          if (points.Count < 3)
                                          return null;
                                          }
                                          }

                                          I find that this refactoring is much easier to read than the original code, as the loop now clearly shows its intent. If you really don't like extra indent level in the loop, you could use a continue:

                                          for (int i = max; i >= min; i--)
                                          {
                                          if (! ShouldPointBeRemoved(points, i))
                                          continue;
                                          points.RemoveAt(i);
                                          if (points.Count < 3)
                                          return null;
                                          }

                                          S 2 Replies 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