Earlier quoted context omitted.
Very inefficient on most array lists (but a very good idea on linked lists)
How many tabs do you use? Less than 50? Then don't worry about the optimization until you have to port it to a PDP10.
No, way more than that.
151–160 of 217 posts
So what's the "correct" way to do this? Clearing the entire array can usually be accomplished easily, but what if you want to remove only items matching some condition? Looping backwards, perhaps? for ( int i=this.MyControl.TabPages.Count-1 ; i >= 0 ; i-- ) { this.MyControl.TabPages.Remove ( this.MyControl.TabPages[i] ); }
filter predicate xsAbout 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…
> It's easy to dismiss people "jiggling things around until they work", as lesser, more inefficient or just plain bad programmers. Just remember that you were once like that too. Sure. But you're not ready for much more than an internship in the software industry (if even that), if that's the phase of programming you're at. The point is that you aren't yet ready to write production code if that's your approach.
But if it's starting to use some advanced OO concept, a new protocol or similar, experimentation and learning-by-doing is a very valid approach, even if you are highly experienced.
If it was not alright to code something without understanding every single layer from the highest to the lowest level, not many programmers would get anything done.
Except maybe Linus Thorvalds or Steve McConnell :-)
That said, I've been guilty of doing stupid things like this many times when I'm tired and just want the damn thing to work. Its amazing the kind of errors you make in situations like that.
OK, fairly newbie coder here.. What would be wrong with just setting a variable to the value of 'this.MyControl.TabPages.Count' outside of the for loop and refering to this?? ie; var x = this.MyControl.TabPages.Count; for ( int i=0 ; i as a quick fix, or if someone did not know while loops or clear function??
When i reaches 2, two tabs have already been removed, so there is only one tab left. So in the loop body we then do:
this.MyControl.TabPages[2] // oh no!
In general, modifying a collection is a bit of a code smell, and a lot of iterator implementations will actually throw exceptions if you try it.OK, fairly newbie coder here.. What would be wrong with just setting a variable to the value of 'this.MyControl.TabPages.Count' outside of the for loop and refering to this?? ie; var x = this.MyControl.TabPages.Count; for ( int i=0 ; i as a quick fix, or if someone did not know while loops or clear function??
As a rule of thumb, don't ever rely on indexation in a collection if you do random deletes. Usually you'll just blow up your app gracelessly. Sometimes, epic failure ensues.
Through some feats of logic we might deduce that there's always a first element, though, until the collection is empty. So you might do this inside the loop: MyControl.TabPages.RemoveAt(0)
Needless to say, calling Remove when you have the bloody index (on IList collections that is) is counter-productive.
And I guess that code would be okay. I mean, if you head to phrase it : let x be the number of elements in the list, take out the head of the list that many times.
However, is it really the fastest way to clear a list? No. If we could access the class internals, we could just replace the store with a new empty array. Voila, O(1) clear and the garbage collector takes out the trash for you.
Generally, though, don't spend time worrying about implementation if you already have one available. When you've done optimizing all of your stuff (which is never the case), then you could go on to suggest changes to the standard library.
So what's the "correct" way to do this? Clearing the entire array can usually be accomplished easily, but what if you want to remove only items matching some condition? Looping backwards, perhaps? for ( int i=this.MyControl.TabPages.Count-1 ; i >= 0 ; i-- ) { this.MyControl.TabPages.Remove ( this.MyControl.TabPages[i] ); }
As I mentioned above, the best way to keep items matching a condition is filter predicate xs
IEnumerable.Where(Func predicate)
It's quite simple to use: var odd_numbers = numbers.Where( n => n%2 == 1 );OK, fairly newbie coder here.. What would be wrong with just setting a variable to the value of 'this.MyControl.TabPages.Count' outside of the for loop and refering to this?? ie; var x = this.MyControl.TabPages.Count; for ( int i=0 ; i as a quick fix, or if someone did not know while loops or clear function??
Say we start with three tabs, so i goes 0, 1, 2. When i reaches 2, two tabs have already been removed, so there is only one tab left. So in the loop body we then do: this.MyControl.TabPages[2] // oh no! In general, modifying a collection is a bit of a code smell, and a lot of iterator implementations will actually throw exceptions if you try it.
var x = this.MyControl.TabPages.Count;
for ( int i=x ; i >0 ; i-- )
{
this.MyControl.TabPages.Remove ( this.MyControl.TabPages[i-1] );
}At the very least, I would have hoped everyone had read through the article.