4 ms·
No, I pretty much just recoiled in horror immediately. Even if I had to avoid .Clear(), there are immediately-obvious, better ways. while ( !thing.Empty() )
by palish 15y ago
No, I pretty much just recoiled in horror immediately.
Even if I had to avoid .Clear(), there are immediately-obvious, better ways.
while ( !thing.Empty() )
thing.Remove( 0 );
In fact, the code isn't just bad; it's risky. What if someone changes "int" to "uint"? It wouldn't even infinite loop / crash... It would remove exactly one element!
- simonsarris 15y agoJust 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.
- deleted 15y ago[deleted]
- deleted 15y ago[deleted]
- danieldk 15y agoBut we are talking about tabs here. Premature optimization is the root of all evil (to throw another cliché).
- lylejohnson 15y agoSpoken by a man who has never encountered a "tabbed notebook"-style UI with multiple rows of tabs, stacked on top of each other. ;)
- lukeschlather 15y agoPremature optimization is the root of all evil, but removing from the beginning of any array-based growable structure rather than the beginning will turn a O(n) operation into an O(n^2) operation. And you don't know what's going into those tabs. That cliché shouldn't be used to excuse positively brain-dead choices.
- danieldk 15y agobut removing from the beginning of any array-based growable structure rather than the beginning will turn a O(n) operation into an O(n^2) operation. I'll bite: in arrays, front deletion (like back deletion) can be done in O(1) time. Keep a pointer to the first element, and just increment it upon front deletion (and free the element if necessary). Obviously, you may want to be smart to avoid memory use getting out of hand. And you don't know what's going into those tabs. If the array stores references (pointers), which you'd expect with tabs, that is orthogonal to the discussion. Whatever is going into those tabs, a pointer is a pointer.
- xentronium 15y ago> I'll bite: in arrays, front deletion (like back deletion) can be done in O(1) time. Keep a pointer to the first element, and just increment it upon front deletion (and free the element if necessary). Obviously, you may want to be smart to avoid memory use getting out of hand. Actually that's how they often do array-based queues and deques. To help with memory use they let the tail pointer overflow through the end (so you could have tail at index 2 and head at index 8).
- deleted 15y ago[deleted]
- sanderjd 15y agoI really like your insight about the accumulation of decisions over the course of a project - I've never thought about it in quite that way, that by having one default decision or the other, your project's destiny is completely different. Picking a sane default answer to that question for a project should be thought of as one of the (many) important jobs of its creator. edit: I really don't understand why the parent comment was downvoted and then deleted.
- palish 15y agoYep, 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... =)
- bad_user 15y agoThere's usually a Remove() with no arguments that depending on the data-structure involved will remove from either the beginning or the end (depending on how it is more efficient for said data structure).
- roel_v 15y agoThis is the second time you said that in this thread, now I can't restrain myself anymore - 'list' != 'array', and it all depends on how they're implemented; I see no reason to assert that for 'most implementations' removing from the end is faster. For a single linked list, it's faster to remove from the start. For a double linked list, it doesn't matter (well it depends, it could be slower). For a 'we call it a list but all containers are really hashes', it doesn't matter. For a regular C array, it depends on how you work with it.
- lurker19 15y agoHe post you are actually responding to answers your questions. Javascript and Python are mentioned.
- burgerbrain 15y ago"Lists" in python are not in fact lists (as the term is traditionally used to refer to linked lists). They are array backed. I expect "arrays" in javascript are similarly arrays... roel_v is correct to point out this misunderstanding.
- tspiteri 15y agoWhat if someone changes "int" to "uint"? It wouldn't even infinite loop / crash... It would remove exactly one element! I can't see that. If i is a uint, at the end of the first cycle, i-- changes i to the maximum uint, then i++ changes i to 0, then i < Count compares Count to 0. It is identical to, if uglier than, the following: while ( 0 < this.MyControl.TabPages.Count ) { this.MyControl.TabPages.Remove ( this.MyControl.TabPages[0] ); }
- palish 15y agoOh, yep. That's correct, thanks.