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. Other Discussions
  3. The Weird and The Wonderful
  4. reviewing old code

reviewing old code

Scheduled Pinned Locked Moved The Weird and The Wonderful
18 Posts 13 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.
  • A AspDotNetDev

    They're both horrors. :(( You don't ever need to return Stuff. Not to mention this "ShuffleNodeList" function is called each time loadStuff is called (I'm guessing it should only be called after everything has been loaded). I wonder... is this code that is using the Umbraco API? Some of that looks familiar. :)

    Thou mewling ill-breeding pignut!

    0 Offline
    0 Offline
    0bx
    wrote on last edited by
    #6

    Yes it's the Umbraco api and it's not the complete code, it returns the list to be used in an other function that generates html in order to make the page look different every time you visit it. Having the ShuffleNodeList was at the end at first... I though I had a good reason to put it in between at first, but looking at it doesn't make sense anymore. :~

    Giraffes are not real.

    A 1 Reply Last reply
    0
    • E ekolis

      WHYYYYY??? If you're declaring Stuff in Page_Load and never doing anything with it, just nuke it all! Or did you not show the code that actually uses Stuff to populate the page or whatever?

      0 Offline
      0 Offline
      0bx
      wrote on last edited by
      #7

      Yes it's used for something of course. I'm not 'that' stupid. :doh: Edit: and I'm not actually calling my variables "Stuff" either in case you're wondering. ;P

      Giraffes are not real.

      S 1 Reply Last reply
      0
      • 0 0bx

        Behold! I found something like this:

        protected void Page_Load(object sender, EventArgs e)
        {
        List Stuff = loadStuff();
        }

        private List loadStuff()
        {
        List Stuff = new List();
        someMoreStuff:
        foreach (Node thing in Node.GetCurrent().Children)
        {
        Stuff.Add(thing);
        }
        Stuff = ShuffleNodeList(Stuff);
        if (Stuff.Count < 100 && Stuff.Count > 0)
        {
        goto someMoreStuff;
        }
        return Stuff;
        }

        It worked... but I changed it so that it never happened:

        protected void Page_Load(object sender, EventArgs e)
        {
        var Stuff = new List();
        Stuff = loadStuff(Stuff);
        }

        private List loadStuff(List Stuff)
        {
        foreach (Node thing in Node.GetCurrent().Children)
        {
        Stuff.Add(thing);
        }
        Stuff = ShuffleNodeList(Stuff);
        if (blablabla)
        {
        loadStuff(Stuff);
        }
        return Stuff;
        }

        Edit: So I changed it again...

        protected void Page_Load(object sender, EventArgs e)
        {
        List Stuff = loadStuff();
        }

        private List loadStuff()
        {
        List Stuff = new List();
        do {
        foreach (Node thing in Node.GetCurrent().Children)
        {
        Stuff.Add(thing);
        }
        } while (Stuff.Count < 100 && Stuff.Count > 0))

        return ShuffleNodeList(Stuff);
        

        }

        Giraffes are not real.

        T Offline
        T Offline
        TorstenFrings
        wrote on last edited by
        #8

        I wonder, why u had to use recursion/goto at all:

        protected void Page_Load(object sender, EventArgs e)
        {
        var Stuff = loadStuff();
        }

        private List loadStuff()
        {
        var Stuff = new List();
        do
        {
        foreach (Node thing in Node.GetCurrent().Children)
        {
        Stuff.Add(thing);
        }
        Stuff = ShuffleNodeList(Stuff);
        }
        while (blablabla);
        return Stuff;
        }

        1 Reply Last reply
        0
        • 0 0bx

          Behold! I found something like this:

          protected void Page_Load(object sender, EventArgs e)
          {
          List Stuff = loadStuff();
          }

          private List loadStuff()
          {
          List Stuff = new List();
          someMoreStuff:
          foreach (Node thing in Node.GetCurrent().Children)
          {
          Stuff.Add(thing);
          }
          Stuff = ShuffleNodeList(Stuff);
          if (Stuff.Count < 100 && Stuff.Count > 0)
          {
          goto someMoreStuff;
          }
          return Stuff;
          }

          It worked... but I changed it so that it never happened:

          protected void Page_Load(object sender, EventArgs e)
          {
          var Stuff = new List();
          Stuff = loadStuff(Stuff);
          }

          private List loadStuff(List Stuff)
          {
          foreach (Node thing in Node.GetCurrent().Children)
          {
          Stuff.Add(thing);
          }
          Stuff = ShuffleNodeList(Stuff);
          if (blablabla)
          {
          loadStuff(Stuff);
          }
          return Stuff;
          }

          Edit: So I changed it again...

          protected void Page_Load(object sender, EventArgs e)
          {
          List Stuff = loadStuff();
          }

          private List loadStuff()
          {
          List Stuff = new List();
          do {
          foreach (Node thing in Node.GetCurrent().Children)
          {
          Stuff.Add(thing);
          }
          } while (Stuff.Count < 100 && Stuff.Count > 0))

          return ShuffleNodeList(Stuff);
          

          }

          Giraffes are not real.

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

          On the no need for recursion and getting rid of the goto, can I add only doing a single shuffle:

          protected void Page_Load(object sender, EventArgs e)
          {
          List Stuff = loadStuff();
          }

          /// Create a list of nodes.
          /// Takes the child nodes from the current node and loads at
          /// least 100 items into the list. If there are currently 30 child nodes
          /// this will return 120 items in the list.
          private List loadStuff()
          {
          List Stuff = new List();
          do {
          foreach (Node thing in Node.GetCurrent().Children)
          {
          Stuff.Add(thing);
          }
          } while (Stuff.Count < 100 && Stuff.Count > 0))

          return ShuffleNodeList(Stuff);
          

          }

          Nice, self contained and easy to follow.


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          0 1 Reply Last reply
          0
          • 0 0bx

            Behold! I found something like this:

            protected void Page_Load(object sender, EventArgs e)
            {
            List Stuff = loadStuff();
            }

            private List loadStuff()
            {
            List Stuff = new List();
            someMoreStuff:
            foreach (Node thing in Node.GetCurrent().Children)
            {
            Stuff.Add(thing);
            }
            Stuff = ShuffleNodeList(Stuff);
            if (Stuff.Count < 100 && Stuff.Count > 0)
            {
            goto someMoreStuff;
            }
            return Stuff;
            }

            It worked... but I changed it so that it never happened:

            protected void Page_Load(object sender, EventArgs e)
            {
            var Stuff = new List();
            Stuff = loadStuff(Stuff);
            }

            private List loadStuff(List Stuff)
            {
            foreach (Node thing in Node.GetCurrent().Children)
            {
            Stuff.Add(thing);
            }
            Stuff = ShuffleNodeList(Stuff);
            if (blablabla)
            {
            loadStuff(Stuff);
            }
            return Stuff;
            }

            Edit: So I changed it again...

            protected void Page_Load(object sender, EventArgs e)
            {
            List Stuff = loadStuff();
            }

            private List loadStuff()
            {
            List Stuff = new List();
            do {
            foreach (Node thing in Node.GetCurrent().Children)
            {
            Stuff.Add(thing);
            }
            } while (Stuff.Count < 100 && Stuff.Count > 0))

            return ShuffleNodeList(Stuff);
            

            }

            Giraffes are not real.

            F Offline
            F Offline
            Francis W Porretto
            wrote on last edited by
            #10

            Chuckle! I think a lot of us do the same, when the occasion arises. I certainly do. Of course, there's no stigma involved in the practice, as long as you remember to test your replacement code thoroughly. Right? Right?

            (As it happens, I also do it to my old, already-published fiction...which causes my readers a bit of concern when they discover it! Well, at least with fiction the "debugging" is less onerous.)

            1 Reply Last reply
            0
            • 0 0bx

              Behold! I found something like this:

              protected void Page_Load(object sender, EventArgs e)
              {
              List Stuff = loadStuff();
              }

              private List loadStuff()
              {
              List Stuff = new List();
              someMoreStuff:
              foreach (Node thing in Node.GetCurrent().Children)
              {
              Stuff.Add(thing);
              }
              Stuff = ShuffleNodeList(Stuff);
              if (Stuff.Count < 100 && Stuff.Count > 0)
              {
              goto someMoreStuff;
              }
              return Stuff;
              }

              It worked... but I changed it so that it never happened:

              protected void Page_Load(object sender, EventArgs e)
              {
              var Stuff = new List();
              Stuff = loadStuff(Stuff);
              }

              private List loadStuff(List Stuff)
              {
              foreach (Node thing in Node.GetCurrent().Children)
              {
              Stuff.Add(thing);
              }
              Stuff = ShuffleNodeList(Stuff);
              if (blablabla)
              {
              loadStuff(Stuff);
              }
              return Stuff;
              }

              Edit: So I changed it again...

              protected void Page_Load(object sender, EventArgs e)
              {
              List Stuff = loadStuff();
              }

              private List loadStuff()
              {
              List Stuff = new List();
              do {
              foreach (Node thing in Node.GetCurrent().Children)
              {
              Stuff.Add(thing);
              }
              } while (Stuff.Count < 100 && Stuff.Count > 0))

              return ShuffleNodeList(Stuff);
              

              }

              Giraffes are not real.

              R Offline
              R Offline
              Robert Heffernan
              wrote on last edited by
              #11

              Gah! You needed that goto for the "Goto Hell" Achievement! I remember when goto and gosub/return were the ONLY way to get around in your program flow, how times have changed!

              1 Reply Last reply
              0
              • 0 0bx

                Yes it's used for something of course. I'm not 'that' stupid. :doh: Edit: and I'm not actually calling my variables "Stuff" either in case you're wondering. ;P

                Giraffes are not real.

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #12

                0bx wrote:

                and I'm not actually calling my variables "Stuff" either

                No? I'm disappointed. I found that rather original. ;P

                1 Reply Last reply
                0
                • 0 0bx

                  Behold! I found something like this:

                  protected void Page_Load(object sender, EventArgs e)
                  {
                  List Stuff = loadStuff();
                  }

                  private List loadStuff()
                  {
                  List Stuff = new List();
                  someMoreStuff:
                  foreach (Node thing in Node.GetCurrent().Children)
                  {
                  Stuff.Add(thing);
                  }
                  Stuff = ShuffleNodeList(Stuff);
                  if (Stuff.Count < 100 && Stuff.Count > 0)
                  {
                  goto someMoreStuff;
                  }
                  return Stuff;
                  }

                  It worked... but I changed it so that it never happened:

                  protected void Page_Load(object sender, EventArgs e)
                  {
                  var Stuff = new List();
                  Stuff = loadStuff(Stuff);
                  }

                  private List loadStuff(List Stuff)
                  {
                  foreach (Node thing in Node.GetCurrent().Children)
                  {
                  Stuff.Add(thing);
                  }
                  Stuff = ShuffleNodeList(Stuff);
                  if (blablabla)
                  {
                  loadStuff(Stuff);
                  }
                  return Stuff;
                  }

                  Edit: So I changed it again...

                  protected void Page_Load(object sender, EventArgs e)
                  {
                  List Stuff = loadStuff();
                  }

                  private List loadStuff()
                  {
                  List Stuff = new List();
                  do {
                  foreach (Node thing in Node.GetCurrent().Children)
                  {
                  Stuff.Add(thing);
                  }
                  } while (Stuff.Count < 100 && Stuff.Count > 0))

                  return ShuffleNodeList(Stuff);
                  

                  }

                  Giraffes are not real.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #13

                  Yep, both are horrors.

                  1 Reply Last reply
                  0
                  • N Nagy Vilmos

                    On the no need for recursion and getting rid of the goto, can I add only doing a single shuffle:

                    protected void Page_Load(object sender, EventArgs e)
                    {
                    List Stuff = loadStuff();
                    }

                    /// Create a list of nodes.
                    /// Takes the child nodes from the current node and loads at
                    /// least 100 items into the list. If there are currently 30 child nodes
                    /// this will return 120 items in the list.
                    private List loadStuff()
                    {
                    List Stuff = new List();
                    do {
                    foreach (Node thing in Node.GetCurrent().Children)
                    {
                    Stuff.Add(thing);
                    }
                    } while (Stuff.Count < 100 && Stuff.Count > 0))

                    return ShuffleNodeList(Stuff);
                    

                    }

                    Nice, self contained and easy to follow.


                    Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                    0 Offline
                    0 Offline
                    0bx
                    wrote on last edited by
                    #14

                    Okay, I knew about the "While" loop, but I didn't know you can do it like this.

                    Giraffes are not real.

                    1 Reply Last reply
                    0
                    • 0 0bx

                      Behold! I found something like this:

                      protected void Page_Load(object sender, EventArgs e)
                      {
                      List Stuff = loadStuff();
                      }

                      private List loadStuff()
                      {
                      List Stuff = new List();
                      someMoreStuff:
                      foreach (Node thing in Node.GetCurrent().Children)
                      {
                      Stuff.Add(thing);
                      }
                      Stuff = ShuffleNodeList(Stuff);
                      if (Stuff.Count < 100 && Stuff.Count > 0)
                      {
                      goto someMoreStuff;
                      }
                      return Stuff;
                      }

                      It worked... but I changed it so that it never happened:

                      protected void Page_Load(object sender, EventArgs e)
                      {
                      var Stuff = new List();
                      Stuff = loadStuff(Stuff);
                      }

                      private List loadStuff(List Stuff)
                      {
                      foreach (Node thing in Node.GetCurrent().Children)
                      {
                      Stuff.Add(thing);
                      }
                      Stuff = ShuffleNodeList(Stuff);
                      if (blablabla)
                      {
                      loadStuff(Stuff);
                      }
                      return Stuff;
                      }

                      Edit: So I changed it again...

                      protected void Page_Load(object sender, EventArgs e)
                      {
                      List Stuff = loadStuff();
                      }

                      private List loadStuff()
                      {
                      List Stuff = new List();
                      do {
                      foreach (Node thing in Node.GetCurrent().Children)
                      {
                      Stuff.Add(thing);
                      }
                      } while (Stuff.Count < 100 && Stuff.Count > 0))

                      return ShuffleNodeList(Stuff);
                      

                      }

                      Giraffes are not real.

                      H Offline
                      H Offline
                      Harley L Pebley
                      wrote on last edited by
                      #15

                      0bx wrote:

                      foreach (Node thing in Node.GetCurrent().Children) { Stuff.Add(thing); }

                      Others have commented on the outer looping constructs. Regarding the inner foreach loop, I'd just get rid of it:

                      Stuff.AddRange(Node.GetCurrent().Children);

                      Cheers.

                      1 Reply Last reply
                      0
                      • 0 0bx

                        Yes it's the Umbraco api and it's not the complete code, it returns the list to be used in an other function that generates html in order to make the page look different every time you visit it. Having the ShuffleNodeList was at the end at first... I though I had a good reason to put it in between at first, but looking at it doesn't make sense anymore. :~

                        Giraffes are not real.

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

                        0bx wrote:

                        it returns the list to be used in an other function

                        But the calling function already has the list, as it was passed in as a parameter, so it does not need to be returned since it already exists at the location it would be returned to. I suppose returning it could make for shorter syntax, but not in the case you have shown.

                        Thou mewling ill-breeding pignut!

                        1 Reply Last reply
                        0
                        • 0 0bx

                          Behold! I found something like this:

                          protected void Page_Load(object sender, EventArgs e)
                          {
                          List Stuff = loadStuff();
                          }

                          private List loadStuff()
                          {
                          List Stuff = new List();
                          someMoreStuff:
                          foreach (Node thing in Node.GetCurrent().Children)
                          {
                          Stuff.Add(thing);
                          }
                          Stuff = ShuffleNodeList(Stuff);
                          if (Stuff.Count < 100 && Stuff.Count > 0)
                          {
                          goto someMoreStuff;
                          }
                          return Stuff;
                          }

                          It worked... but I changed it so that it never happened:

                          protected void Page_Load(object sender, EventArgs e)
                          {
                          var Stuff = new List();
                          Stuff = loadStuff(Stuff);
                          }

                          private List loadStuff(List Stuff)
                          {
                          foreach (Node thing in Node.GetCurrent().Children)
                          {
                          Stuff.Add(thing);
                          }
                          Stuff = ShuffleNodeList(Stuff);
                          if (blablabla)
                          {
                          loadStuff(Stuff);
                          }
                          return Stuff;
                          }

                          Edit: So I changed it again...

                          protected void Page_Load(object sender, EventArgs e)
                          {
                          List Stuff = loadStuff();
                          }

                          private List loadStuff()
                          {
                          List Stuff = new List();
                          do {
                          foreach (Node thing in Node.GetCurrent().Children)
                          {
                          Stuff.Add(thing);
                          }
                          } while (Stuff.Count < 100 && Stuff.Count > 0))

                          return ShuffleNodeList(Stuff);
                          

                          }

                          Giraffes are not real.

                          E Offline
                          E Offline
                          englebart
                          wrote on last edited by
                          #17

                          What if Stuff.Count never exceeds 100 items? Both versions seem like infinite loops. Recursion would be superior here because it will at least blow the call stack in a reasonable time frame vs. waiting for the application container (IIS?) to kill the non-responsive thread. I usually include a safety valve for that kind of code: int maxReps = 100; blalbalba && (maxReps-- > 0) I always use safety valves when writing code that "should" converge to an answer.

                          1 Reply Last reply
                          0
                          • 0 0bx

                            Behold! I found something like this:

                            protected void Page_Load(object sender, EventArgs e)
                            {
                            List Stuff = loadStuff();
                            }

                            private List loadStuff()
                            {
                            List Stuff = new List();
                            someMoreStuff:
                            foreach (Node thing in Node.GetCurrent().Children)
                            {
                            Stuff.Add(thing);
                            }
                            Stuff = ShuffleNodeList(Stuff);
                            if (Stuff.Count < 100 && Stuff.Count > 0)
                            {
                            goto someMoreStuff;
                            }
                            return Stuff;
                            }

                            It worked... but I changed it so that it never happened:

                            protected void Page_Load(object sender, EventArgs e)
                            {
                            var Stuff = new List();
                            Stuff = loadStuff(Stuff);
                            }

                            private List loadStuff(List Stuff)
                            {
                            foreach (Node thing in Node.GetCurrent().Children)
                            {
                            Stuff.Add(thing);
                            }
                            Stuff = ShuffleNodeList(Stuff);
                            if (blablabla)
                            {
                            loadStuff(Stuff);
                            }
                            return Stuff;
                            }

                            Edit: So I changed it again...

                            protected void Page_Load(object sender, EventArgs e)
                            {
                            List Stuff = loadStuff();
                            }

                            private List loadStuff()
                            {
                            List Stuff = new List();
                            do {
                            foreach (Node thing in Node.GetCurrent().Children)
                            {
                            Stuff.Add(thing);
                            }
                            } while (Stuff.Count < 100 && Stuff.Count > 0))

                            return ShuffleNodeList(Stuff);
                            

                            }

                            Giraffes are not real.

                            K Offline
                            K Offline
                            KP Lee
                            wrote on last edited by
                            #18

                            How about?:

                            protected void Page_Load(object sender, EventArgs e)
                            {
                            List Stuff = new List();
                            do {
                            foreach (Node thing in Node.GetCurrent().Children)
                            Stuff.Add(thing);
                            } while (Stuff.Count < 100 && Stuff.Count > 0))

                            Stuff = ShuffleNodeList(Stuff);
                            

                            }

                            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