About the McConnell quote: "Inefficient programmers tend to experiment randomly until they find a combination that seems to work." The essence of this quote is being passed around quite often these days. When you first start programming, you generally have no idea what the hell you are doing. You learn all these strange, abstract concepts best, by experimenting. It's easy to dismiss people "jiggling things around unt…
Understood, but I would caution that even an inexperienced, inefficient programmer should still understand what their code does and why it works . The best way the more experienced can exercise patience is by giving inexperienced programmers enough time to figure it out rather than pressuring them to flip random switches frantically.
What's wrong with this code, really?
51–60 of 217 posts
Re: What's wrong with this code, really?
#52What's wrong with the code, then, is that it was written under a little too much pressure for the developer to think clearly. for ( int i=0 ; i
Why not just: while (this.MyControl.TabPages.Count > 0) { this.MyControl.TabPages.Remove ( 0 ); }
Re: What's wrong with this code, really?
#53Earlier quoted context omitted.
Why not just: while (this.MyControl.TabPages.Count > 0) { this.MyControl.TabPages.Remove ( 0 ); }
Very inefficient on most array lists (but a very good idea on linked lists)
Less than 50?
Then don't worry about the optimization until you have to port it to a PDP10.
Re: What's wrong with this code, really?
#54Earlier quoted context omitted.
Just for the record, your method is inefficient for a lot of implementations of lists/arrays, where it is often far faster to remove from the end than to remove from the beginning.
Yep, that's correct. I wish "thing.Length - 1" were as clear as "0", but it's not. Besides, if you really cared about efficiency, you wouldn't be using growable containers... =)
Re: What's wrong with this code, really?
#55Earlier quoted context omitted.
Correct. I think the logical thing to do would be to delete from the end so that the index is always valid. for (int i=this.MyControl.TabPages.Count - 1; i > 0; i--) { this.MyControl.TabPages.Remove(this.MyControl.TabPages[i]); } Imagine the count is 2. The first iteration you delete item 1, the second you delete item 0, and then the loop exits. EDIT: Actually, as someone else pointed out, it's clearer to use a while…
Actually, you will never delete the final element with that code. I think what you meant was: for (int i=this.MyControl.TabPages.Count; i > 0; i--) { this.MyControl.TabPages.Remove(this.MyControl.TabPages[i-1]); }
for (int i=this.MyControl.TabPages.Count - 1; i >= 0; i--) {
this.MyControl.TabPages.Remove(this.MyControl.TabPages[i]);
}
Though a simple while loop is much easier to follow, even if its less efficient than removing the elements in reverse.Re: What's wrong with this code, really?
#56Re: What's wrong with this code, really?
#57Earlier quoted context omitted.
The problem is that .Count decrements every time you .Remove(). Either change [i] to [0] in your code, or initialize i with .Count-1 and decrement in the loop.
For the record, the second way is typically better: It is usually more (sometimes much more) efficient to remove from the end of a list/array than to remove from the beginning.
Re: What's wrong with this code, really?
#58About the McConnell quote: "Inefficient programmers tend to experiment randomly until they find a combination that seems to work." The essence of this quote is being passed around quite often these days. When you first start programming, you generally have no idea what the hell you are doing. You learn all these strange, abstract concepts best, by experimenting. It's easy to dismiss people "jiggling things around unt…
They don't have a mind of their own, and programming isn't magic. Opaque languages, libraries and APIs don't help the situation either. I wonder how many programmers start out under the assumption that computers are more-or-less magic?
Re: What's wrong with this code, really?
#59Re: What's wrong with this code, really?
#60Earlier quoted context omitted.
Actually, you will never delete the final element with that code. I think what you meant was: for (int i=this.MyControl.TabPages.Count; i > 0; i--) { this.MyControl.TabPages.Remove(this.MyControl.TabPages[i-1]); }
I prefer: for (int i=this.MyControl.TabPages.Count - 1; i >= 0; i--) { this.MyControl.TabPages.Remove(this.MyControl.TabPages[i]); } Though a simple while loop is much easier to follow, even if its less efficient than removing the elements in reverse.
while (MyControl.TabPages.Count > 0) { MyControl.TabPages.RemoveAt(MyControl.TabPages.Count-1); }
For loop are nothing more than while loops with:
(1) an assignment (int i = MyControl.TabPages.Count in this case)
(2) an extra command (i-- in this case) added to the end