Live data from Hacker News

What's wrong with this code, really?

cvmountain.com

151–160 of 217 posts

Re: What's wrong with this code, really?

#151
post #53

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.

> Less than 50?

No, way more than that.

Re: What's wrong with this code, really?

#152
post #53

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.

> Less than 50?

No, over nine thousands.

Re: What's wrong with this code, really?

#153

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

Re: What's wrong with this code, really?

#154
post #27

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…

> 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.

That depends what you are jiggling around :-) If you can't immediately make basic loops work, then yes, you shouldn't be anywhere near production code yet.

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 :-)

Re: What's wrong with this code, really?

#155
Whenever a loop index is modified inside a for loop, it should raise immediate red flags. Also, with intellisense in Visual Studio, it shouldn't take more than a few seconds to check if there is .Clear() or .RemoveAll() method.

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.

Re: What's wrong with this code, really?

#156

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.

Re: What's wrong with this code, really?

#157

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??

Well, what happens when you try to remove the 10th page when there's only 1 left?

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.

Re: What's wrong with this code, really?

#158
post #153

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

In case you don't speak Haskell:

    IEnumerable.Where(Func predicate)
It's quite simple to use:

    var odd_numbers = numbers.Where( n => n%2 == 1 );

Re: What's wrong with this code, really?

#159

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.

OK, i didn't think through the code.. rather i meant to iterate downwards;

    var x = this.MyControl.TabPages.Count;
    for ( int i=x ; i >0 ; i-- )
    {
       this.MyControl.TabPages.Remove ( this.MyControl.TabPages[i-1] );
    }
Post reply on HN