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. Embarrassing code admission of the day (or why C.S. is good for you)

Embarrassing code admission of the day (or why C.S. is good for you)

Scheduled Pinned Locked Moved The Lounge
questioncsharpandroidcomdesign
63 Posts 29 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.
  • J Julien Villers

    Yup, it's not a very good choice (ArrayList was nicer, but not generic). On the other hand, consider all the time gained by typing List<> instead of DynamicArray<> :D

    'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

    M Offline
    M Offline
    Mladen Jankovic
    wrote on last edited by
    #41

    STL uses vector as name for dynamic arrays, that's just two letters more and much less misleading :)

    L 1 Reply Last reply
    0
    • M Mladen Jankovic

      STL uses vector as name for dynamic arrays, that's just two letters more and much less misleading :)

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

      How is that "much less misleading"? It has "mathematical vector" written all over it, but it's not even close to that. Meanwhile why should a list, sans "linked", be linked?

      M 1 Reply Last reply
      0
      • K Keith Barrow

        Ah. Now you see, that is another example as to why programming is hard.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-
        A Dead ringer for Kate Winslett[^]

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #43

        Keith Barrow wrote:

        another example as to why programming is hard

        Programming is easy. Doing it properly is hard.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        1 Reply Last reply
        0
        • L Lost User

          How is that "much less misleading"? It has "mathematical vector" written all over it, but it's not even close to that. Meanwhile why should a list, sans "linked", be linked?

          M Offline
          M Offline
          Mladen Jankovic
          wrote on last edited by
          #44

          harold aptroot wrote:

          How is that "much less misleading"?

          Because it is not in direct collision with another widely used data structure and mathematical vector is not used as much as the other tow (list/array) in programming.

          harold aptroot wrote:

          Meanwhile why should a list, sans "linked", be linked?

          It doesn't have to be, theoretically, but in practice when someone mention list, one usually assume it is a linked list and everything that goes with it (costs/benefits), and thus it is misleading. OP's example illustrates my point and I'll quote the guy before you how later edited his post:

          Well, in .NET a List is well documented, so list[0] should be O(1), while list[n-1] should be O(n).

          Add me to this list and you have 3 guys that were misled by the class name. Even though some of them were aware of how List<> is actually implemented.

          L 1 Reply Last reply
          0
          • T TheGreatAndPowerfulOz

            Assuming this is C#, removing the first item in a list creates an entirely new list, which I think is a bug in the underlying list code since removing the first item should just rebase the list head at the next item. I think the underlying list code uses an array, believe it or not. It's not a "bug" per se, unless you consider taking a very long time to do a simple operation a bug.

            If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
            You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #45

            ahmed zahmed wrote:

            removing the first item in a list creates an entirely new list,

            :wtf: seriously?

            image processing toolkits | batch image processing

            P T 2 Replies Last reply
            0
            • C Chris Losinger

              ahmed zahmed wrote:

              removing the first item in a list creates an entirely new list,

              :wtf: seriously?

              image processing toolkits | batch image processing

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #46

              Well not quite. This is what it does internally:

              Array.Copy(this._items, index + 1, this._items, index, this._size - index);

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              C 1 Reply Last reply
              0
              • M Mladen Jankovic

                harold aptroot wrote:

                How is that "much less misleading"?

                Because it is not in direct collision with another widely used data structure and mathematical vector is not used as much as the other tow (list/array) in programming.

                harold aptroot wrote:

                Meanwhile why should a list, sans "linked", be linked?

                It doesn't have to be, theoretically, but in practice when someone mention list, one usually assume it is a linked list and everything that goes with it (costs/benefits), and thus it is misleading. OP's example illustrates my point and I'll quote the guy before you how later edited his post:

                Well, in .NET a List is well documented, so list[0] should be O(1), while list[n-1] should be O(n).

                Add me to this list and you have 3 guys that were misled by the class name. Even though some of them were aware of how List<> is actually implemented.

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

                Well I do a lot of graphics, plenty of vectors around, but yea I guess they're not as common outside of graphics. I don't really like "vector" or "List" as names for a dynamic array. If they'd all just call it an ArrayList, that would make it really obvious. As for people usually assuming lists to be linked .. when I hear list I just think of "ordered bunch of stuff". Then when I start thinking about it I never assume it to be implemented as linked list, because it never is.

                1 Reply Last reply
                0
                • P Pete OHanlon

                  Well not quite. This is what it does internally:

                  Array.Copy(this._items, index + 1, this._items, index, this._size - index);

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #48

                  ah. it's one of those lists: an array in a fancy dress.

                  image processing toolkits | batch image processing

                  P 1 Reply Last reply
                  0
                  • C Chris Losinger

                    ah. it's one of those lists: an array in a fancy dress.

                    image processing toolkits | batch image processing

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #49

                    Yup. It's a generic ArrayList.

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    1 Reply Last reply
                    0
                    • E Ennis Ray Lynch Jr

                      Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                      //init the list and fill it
                      List fakeList = new List();
                      //Find the subtle bug
                      while (fakeList.Count > 0) {
                      double temp = fakeList[0];
                      //..do something
                      fakeList.RemoveAt(0);
                      }

                      Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                      G Offline
                      G Offline
                      Gary Wheeler
                      wrote on last edited by
                      #50

                      The RemoveAt(0) call makes a copy of the list data, minus the first element, on each iteration. The algorithm here is actually written for a linked list. I've always thought the List class was stupidly named since it's really a resizable array, rather than a (linked) list.

                      Software Zen: delete this;

                      1 Reply Last reply
                      0
                      • E Ennis Ray Lynch Jr

                        Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                        //init the list and fill it
                        List fakeList = new List();
                        //Find the subtle bug
                        while (fakeList.Count > 0) {
                        double temp = fakeList[0];
                        //..do something
                        fakeList.RemoveAt(0);
                        }

                        Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #51

                        With the hints ... I get it. RemoveAt(0) causes the whole thing to be shuffled which is pretty expensive. Unless there's some other reason why you need to clean up as you go, you should just foreach the whole list. Or if it's actually a queue, use a Queue.

                        1 Reply Last reply
                        0
                        • C Chris Losinger

                          ahmed zahmed wrote:

                          removing the first item in a list creates an entirely new list,

                          :wtf: seriously?

                          image processing toolkits | batch image processing

                          T Offline
                          T Offline
                          TheGreatAndPowerfulOz
                          wrote on last edited by
                          #52

                          well, like I said, and Pete confirmed, it's underlying implementation is an array.

                          If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
                          You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

                          1 Reply Last reply
                          0
                          • E Ennis Ray Lynch Jr

                            Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                            //init the list and fill it
                            List fakeList = new List();
                            //Find the subtle bug
                            while (fakeList.Count > 0) {
                            double temp = fakeList[0];
                            //..do something
                            fakeList.RemoveAt(0);
                            }

                            Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                            Y Offline
                            Y Offline
                            YvesDaoust
                            wrote on last edited by
                            #53

                            I assume your concern is about the whole operation being O(N^2) when deleting at the front, instead of the O(N) you can achieve when deleting at the back. Is it ? I don't use to call such a misuse a bug, given that the effect is functionally correct. But you are right, if the rest of the processing of the list remains below O(N^2), you can call this a performance bug. BTW, I wonder how many tons of hidden similar deficiencies you can find in modern software. PS: the MS documentation does not really give hints on the complexity of the operations on containers, you have to educated-guess. Shame on them.

                            1 Reply Last reply
                            0
                            • P Pete OHanlon

                              Took me a moment or two to spot that. Couldn't really see it until I thought it through. Good catch - how did you find it? For others - what happens when you remove at 0? How is this handled in terms of resizing when you remove from the start of the list. As a comparison, remove from the last position instead (ok, it's not the same logical code, but it shows timings).

                              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                              M Offline
                              M Offline
                              Matthys Terblanche
                              wrote on last edited by
                              #54

                              Hi, why not iterate through it and clear the list after the loop? Since the items are all deleted anyway, wont that save time?

                              1 Reply Last reply
                              0
                              • E Ennis Ray Lynch Jr

                                Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                //init the list and fill it
                                List fakeList = new List();
                                //Find the subtle bug
                                while (fakeList.Count > 0) {
                                double temp = fakeList[0];
                                //..do something
                                fakeList.RemoveAt(0);
                                }

                                Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                M Offline
                                M Offline
                                M Hussain
                                wrote on last edited by
                                #55

                                You can't remove at 0 index because it is being used in above variable. So to remove the 0 index object remove this line;

                                double temp = fakeList[0];

                                1 Reply Last reply
                                0
                                • E Ennis Ray Lynch Jr

                                  Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                  //init the list and fill it
                                  List fakeList = new List();
                                  //Find the subtle bug
                                  while (fakeList.Count > 0) {
                                  double temp = fakeList[0];
                                  //..do something
                                  fakeList.RemoveAt(0);
                                  }

                                  Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                  Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                  G Offline
                                  G Offline
                                  gritter55
                                  wrote on last edited by
                                  #56

                                  Since you just created the list, its count is zero so it will never step into the while loop. Is it that simple?

                                  1 Reply Last reply
                                  0
                                  • J Julien Villers

                                    Well, in .NET a List is well documented, so list[0] should be O(1), while list[n-1] is also 0(1) (see post below for MSDN link). A .NET List is supposed to be a dynamic array, with the expected performance of an array for single element access. So reversing the loop shown here would not even cause the access speed to be bad, but even if it were bad, it would not be as bad as the constant resizing (RemoveAt(0)) would be. Morality: use a Queue (or not).

                                    'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                                    X Offline
                                    X Offline
                                    xtofl
                                    wrote on last edited by
                                    #57

                                    From the msdn (msdn entry on List.RemoveAt) "This method is an O(n) operation, where n is (Count - index)." Keeping Count as close as possible to index may improve performance with a factor O(n^2) :)

                                    1 Reply Last reply
                                    0
                                    • K Keith Barrow

                                      Ah. Now you see, that is another example as to why programming is hard.

                                      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                                      -Or-
                                      A Dead ringer for Kate Winslett[^]

                                      R Offline
                                      R Offline
                                      Ralph Little
                                      wrote on last edited by
                                      #58

                                      It also seems to be a demonstration that using stock black-box objects where you don't know the underlying implementation has its faults. Same goes for portability. One particular implementation might be great on one platform but appalling on another. I'm not a big fan of the stl for that reason although I do like the underlying idea.

                                      1 Reply Last reply
                                      0
                                      • E Ennis Ray Lynch Jr

                                        Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                        //init the list and fill it
                                        List fakeList = new List();
                                        //Find the subtle bug
                                        while (fakeList.Count > 0) {
                                        double temp = fakeList[0];
                                        //..do something
                                        fakeList.RemoveAt(0);
                                        }

                                        Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                        W Offline
                                        W Offline
                                        Waldemar Sauer
                                        wrote on last edited by
                                        #59

                                        This is not necessarily a bug or performance problem. Never make an assessment on performance without using a profiler first. For all we know, this code could commonly execute on 0 or 1 items, in which case, there is not much wrong with the code above. What if the above is in a message pump where other threads are allowed to peak at the queue head? That said, with the way it is written above, I probably would have gone for the "foreach(){} fakeList.Clear();" construct.

                                        1 Reply Last reply
                                        0
                                        • E Ennis Ray Lynch Jr

                                          Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                          //init the list and fill it
                                          List fakeList = new List();
                                          //Find the subtle bug
                                          while (fakeList.Count > 0) {
                                          double temp = fakeList[0];
                                          //..do something
                                          fakeList.RemoveAt(0);
                                          }

                                          Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                          E Offline
                                          E Offline
                                          ekolis
                                          wrote on last edited by
                                          #60

                                          The while loop will never execute, as fakeList's initial length is zero.

                                          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