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

    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

    D Offline
    D Offline
    Dalek Dave
    wrote on last edited by
    #3

    Were they put in for error control?

    ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

    S 1 Reply Last reply
    0
    • N Nagy Vilmos

      Super Lloyd wrote:

      no less than 2 gotos in my C# code

      If it was no less than 2, then it may have been more...


      Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

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

      maybe... :rolleyes:

      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.

      N 1 Reply Last reply
      0
      • S Super Lloyd

        maybe... :rolleyes:

        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.

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #5

        I had to 'have words' with one fo my minions today for using them there things. We're working in C# ffs, there was no need at all.


        Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

        S 1 Reply Last reply
        0
        • D Dalek Dave

          Were they put in for error control?

          ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

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

          I'm (not) sorry to say that I handle such condition with try {} finally {} so, no, no error handling! Muhahaha.... ;P

          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.

          D 1 Reply Last reply
          0
          • S Super Lloyd

            I'm (not) sorry to say that I handle such condition with try {} finally {} so, no, no error handling! Muhahaha.... ;P

            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.

            D Offline
            D Offline
            Dalek Dave
            wrote on last edited by
            #7

            I often use

            On Error GoTo MyErrorTrap

            ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

            S A 2 Replies Last reply
            0
            • N Nagy Vilmos

              I had to 'have words' with one fo my minions today for using them there things. We're working in C# ffs, there was no need at all.


              Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

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

              I thought about it and... despite my initial inflamatory intention I'm thinking we could both gain by showing you what I did, who knows one of 2 thing might even happen!! 1. maybe you'll think this is a good goto! 2. maybe you'll give me good work around so, here you go (veil your eyes, code in the lounge) please remove the goto in the nice fashion or hold your peace forever!

              for(...)
              {
              avar = calculation()
              if(avar == someValue)
              goto doStuff;
              avar2 = someOtherCalculation();
              if(avar2 == someValue2)
              goto doStuff;
              continue;
              doStuff:;
              localVar.UpdateNicely();
              }

              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.

              A S R 3 Replies Last reply
              0
              • D Dalek Dave

                I often use

                On Error GoTo MyErrorTrap

                ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

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

                Ok, maybe I should say what I did, so maybe an anti-goto purist can give me tips on how to write goto-less nice code in such circumstances... (or become less anti goto) so here we go, that's what I wrote (veil your eyes, code in the lounge!)

                for(...)
                {
                avar = calculation()
                if(avar == someValue)
                goto doStuff;
                avar2 = someOtherCalculation();
                if(avar2 == someValue2)
                goto doStuff;
                continue;
                doStuff:;
                localVar.UpdateNicely();
                }

                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.

                D L P M 4 Replies Last reply
                0
                • S Super Lloyd

                  Ok, maybe I should say what I did, so maybe an anti-goto purist can give me tips on how to write goto-less nice code in such circumstances... (or become less anti goto) so here we go, that's what I wrote (veil your eyes, code in the lounge!)

                  for(...)
                  {
                  avar = calculation()
                  if(avar == someValue)
                  goto doStuff;
                  avar2 = someOtherCalculation();
                  if(avar2 == someValue2)
                  goto doStuff;
                  continue;
                  doStuff:;
                  localVar.UpdateNicely();
                  }

                  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.

                  D Offline
                  D Offline
                  Dalek Dave
                  wrote on last edited by
                  #10

                  Repost! :)

                  ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

                  1 Reply Last reply
                  0
                  • D Dalek Dave

                    I often use

                    On Error GoTo MyErrorTrap

                    ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

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

                    Delete this post before everybody sees and you are shamed!

                    [Forum Guidelines]

                    D 1 Reply Last reply
                    0
                    • S Super Lloyd

                      Ok, maybe I should say what I did, so maybe an anti-goto purist can give me tips on how to write goto-less nice code in such circumstances... (or become less anti goto) so here we go, that's what I wrote (veil your eyes, code in the lounge!)

                      for(...)
                      {
                      avar = calculation()
                      if(avar == someValue)
                      goto doStuff;
                      avar2 = someOtherCalculation();
                      if(avar2 == someValue2)
                      goto doStuff;
                      continue;
                      doStuff:;
                      localVar.UpdateNicely();
                      }

                      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
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.

                      for (...) {
                      if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
                      }

                      Readability is the key concern here. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                      D S A 4 Replies Last reply
                      0
                      • A AspDotNetDev

                        Delete this post before everybody sees and you are shamed!

                        [Forum Guidelines]

                        D Offline
                        D Offline
                        Dalek Dave
                        wrote on last edited by
                        #13

                        Early 80's programming is in my blood! I just can't let go of them goto statements :)

                        ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.

                          for (...) {
                          if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
                          }

                          Readability is the key concern here. :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                          D Offline
                          D Offline
                          Dalek Dave
                          wrote on last edited by
                          #14

                          That's actually a nice bit of code. I have much to learm :sigh:

                          ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

                          L 1 Reply Last reply
                          0
                          • S Super Lloyd

                            Ok, maybe I should say what I did, so maybe an anti-goto purist can give me tips on how to write goto-less nice code in such circumstances... (or become less anti goto) so here we go, that's what I wrote (veil your eyes, code in the lounge!)

                            for(...)
                            {
                            avar = calculation()
                            if(avar == someValue)
                            goto doStuff;
                            avar2 = someOtherCalculation();
                            if(avar2 == someValue2)
                            goto doStuff;
                            continue;
                            doStuff:;
                            localVar.UpdateNicely();
                            }

                            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.

                            P Offline
                            P Offline
                            Peter_in_2780
                            wrote on last edited by
                            #15

                            How about

                            for(...)
                            {
                            avar = calculation()
                            if(avar != someValue)
                            {
                            avar2 = someOtherCalculation();
                            if(avar2 != someValue2)
                            continue;
                            }
                            localVar.UpdateNicely();
                            }

                            The point is basically that

                            if (something)
                            goto somewhere
                            more_stuff;
                            somewhere:

                            is equivalent to

                            if (!something)
                            more_stuff;

                            and you've just got two of them nested. Cheers from one who thinks coding standards are generally a good thing but do not represent ultimate authority. Peter

                            Software rusts. Simon Stephenson, ca 1994.

                            S 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.

                              for (...) {
                              if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
                              }

                              Readability is the key concern here. :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

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

                              well, I wanted to use some simple pseudo code, but the statement "var1 = calculation()" is really a multi inner variable multiline statement with a more complex statement. I guess I can replace it with a private static variable with heaps of ref variable just or the sake of writing such statement as you sugest, but I won't!

                              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
                              • S Super Lloyd

                                I thought about it and... despite my initial inflamatory intention I'm thinking we could both gain by showing you what I did, who knows one of 2 thing might even happen!! 1. maybe you'll think this is a good goto! 2. maybe you'll give me good work around so, here you go (veil your eyes, code in the lounge) please remove the goto in the nice fashion or hold your peace forever!

                                for(...)
                                {
                                avar = calculation()
                                if(avar == someValue)
                                goto doStuff;
                                avar2 = someOtherCalculation();
                                if(avar2 == someValue2)
                                goto doStuff;
                                continue;
                                doStuff:;
                                localVar.UpdateNicely();
                                }

                                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.

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

                                No excuse!

                                for(...)
                                {
                                if(calc() == v || calc2() == v2)
                                {
                                localVar.UpdateNicely();
                                }
                                }

                                [Forum Guidelines]

                                1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.

                                  for (...) {
                                  if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
                                  }

                                  Readability is the key concern here. :)

                                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

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

                                  Dang, you beat me.

                                  [Forum Guidelines]

                                  L 1 Reply Last reply
                                  0
                                  • P Peter_in_2780

                                    How about

                                    for(...)
                                    {
                                    avar = calculation()
                                    if(avar != someValue)
                                    {
                                    avar2 = someOtherCalculation();
                                    if(avar2 != someValue2)
                                    continue;
                                    }
                                    localVar.UpdateNicely();
                                    }

                                    The point is basically that

                                    if (something)
                                    goto somewhere
                                    more_stuff;
                                    somewhere:

                                    is equivalent to

                                    if (!something)
                                    more_stuff;

                                    and you've just got two of them nested. Cheers from one who thinks coding standards are generally a good thing but do not represent ultimate authority. Peter

                                    Software rusts. Simon Stephenson, ca 1994.

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

                                    Indeed that will do! :thumbsup: :) However.... I do prefer goto to multiple nested statement! ;P

                                    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.

                                    D P 2 Replies Last reply
                                    0
                                    • A AspDotNetDev

                                      Dang, you beat me.

                                      [Forum Guidelines]

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #20

                                      the advantage of a keyboard with a very short cable. :)

                                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                      1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        Indeed that will do! :thumbsup: :) However.... I do prefer goto to multiple nested statement! ;P

                                        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.

                                        D Offline
                                        D Offline
                                        Dalek Dave
                                        wrote on last edited by
                                        #21

                                        It's neater.

                                        ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

                                        S 1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          You really should read part 2 of your Language Reference Manual, in particular the chapters on logical operators and short-circuitry.

                                          for (...) {
                                          if (calculation()==someValue || someOtherCalculation()==someValue2) localVar.UpdateNicely();
                                          }

                                          Readability is the key concern here. :)

                                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

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

                                          there is the matter of avoiding goto, but this is no big deal, the real challenge is to do it in aesthetic and readable way! Well, I guess it's a subjective matter... But just to give you better chance, here is the real code. I think goto is the most elegant / beautiful solution. I invite you to show me the beauty of avoiding goto! (Although.. being a subjective thing... well....) Here we go:

                                          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 test above fail
                                          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 Thursday, September 9, 2010 8:31 PM

                                          A 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