- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I would argue that the FOR loop is pretty much the first thing you should learn in any language. This coder clearly disagrees.
Admin
The comment saves it a little.
Admin
34,598th (i.e. last)
Admin
Come on. Everybody knows this situation calls for:
i = dtModules.Rows.Count + 1; //used to jump out of the for
It's way more portable and doesn't rely on magistical numbers.
Admin
Maybe he was confused as to which number would be just above what would fit in a 2 byte storage (32,768). Of course, that only deals with the seemingly randomness of the number and not the insane ignorance of basic concepts.
Admin
Almighty save us all if dtModules ever contains 34598 rows.
Admin
Bravo, that's the first WTF that's actually made me laugh out loud in a while.
Admin
What the heck, at least he wrote a comment that explained why he was doing it.
An Error Occurred An Error Occurred
Admin
Why use addition when for loops can be used to solve anything.
Admin
Admin
If it was C++, the removal method wouldn't be as succinct. The preferred idiom would be some crap like:
dtModules.Rows.back_remover(dtModules.Rows.end_iter(), dtModules.Rows.end_iter() - 1);
And this code would of course be hidden in a functor class that requires half a dozen templated parameters to initialise it.
I fscking hate C++. In fact I hate it so much I've started twitching uncontrollably. Can someone pull the screens round me and find the nurse?
Admin
[quote user="eeth"]Almighty save us all if dtModules ever contains 34598 rows.
But, using a foreach over a collection, then removing an item from that collection during the loop, is also fail.
Admin
#define END_FOR_LOOP 34598
for (int i = 0; i < dtModules.Rows.Count; i++) { if (dtModules.Rows[i]["id"].ToString() == ID.ToString()) { dtModules.Rows.RemoveAt(i); i = END_FOR_LOOP; //used to jump out of the for } }
There, fixed.
Admin
WTF is wrong with a for loop?
captcha: nulla -- Valley girl for Null (i.e. "That variable was so nulla, it like gave me an Object reference not set to an object error.").
Admin
1: If my memory serves, C++ has no ToString() 2: Conceded. 3: If they're not numbers then they're already string and don't need the ToString(). 4: Hence the initial invocation of the Almighty!
Admin
probably he first tried: i = Integer.MAX_VALUE;
but found out that this did not work as expected. Naturally i = 34598 was the next choice!
Admin
Nothing's wrong with a for loop, but a foreach is generally more efficient.
Admin
Whilst I share your hate of template rubbish like that, which seems to have infected a lot of the standard ideas for C++, I do feel like pointing out that it's not the fault of the language. It's the fault of the libraries, including the standard library.
Qt's list container has a lovely removal function - http://doc.trolltech.com/4.6/qlist.html#removeAt (I can't link it without being marked as spam). It's short, obvious and doesn't vomit template code all over your face and torso.
Anyway, the article code is probably C#.
Admin
What happens if there are two objects with the corresponding ID? D:
Admin
Admin
You can make a ToString function in C++.
Top computer scientists across the world have discussed the possibility that there may be other data types than just ints and strings.
Admin
Or, you know:
Admin
I'd go for
try { for (int i = 0; i < dtModules.Rows.Count; i++) { if (dtModules.Rows[i]["id"].ToString() == ID.ToString()) { dtModules.Rows.RemoveAt(i); throw new BreakException(); } } } catch {}
Admin
That only matters if you continue in the foreach, so removing and then break works.
Admin
Absolutely brilliant.
Admin
Whomever wrote this for loop didn't have the skills to write a ToString() in C++.
There are absolutely more data types than integers and strings. That's not what I said, though. And fyi, even strings reduce to numeric representations.
Admin
I disagree with number 1 for a different reason. If you use a foreach, you generate an enumerator. While iterating on the enumerator you cannot modify the original collection. At least you can't in C#, it generates a runtime exception.
TRWTF is the i=34598, the string comparison may be fail, but we don't actually know what the data type of the second dimension of the dictionary is, so it could actually be a reference type, in which case, the code maybe correct in doing a ToString() in order to compare it to a number.
Admin
1)If the source is Java, foreach did not exists until 1.6 2)Even more puzzling....is it really checking to see if the String references are the same? Again, assuming Java, shouldn't it be
if Strings are really required for the comparison... 3)Which makes point 2 even weirder...why bother with toString instead just doing the quick arithmetic comparison? 4)I feel better about my day now...but not by much.
3rd attempt...
Admin
The programmer needs the index of the item in order to remove it, which is not possible with foreach.
Admin
for (int i = dtModules.Rows.Count-1; i && dtModules.Rows[i]["id"].ToString() != ID.ToString() ; i--); if (dtModules.Rows[i]["id"].ToString() == ID.ToString()) dtModules.Rows.RemoveAt(i);
I know, is a little weird.
Admin
In C#, all strings with the same "contents" refer to the same object. C# strings have an .Equals() method too, by the way.
Admin
Sorry...that should be 1.5
Admin
TRWTF is that none of you geniuses realize that a break statement is just a goto in disguise. Thank dog the original coder was able to avoid that evil...
Admin
So is a for-loop
Admin
Admin
Pfff. ON ERROR RESUME NEXT
Admin
Admin
Admin
Easier to read, but if you'll check it out you'll find that a foreach usually takes longer to execute than a for loop. Although picking one over the other is such a micro-optimization that's its laughable.
Admin
Amateurs! OBVIOUSLY you do it best by:
Admin
1: It does after you implement it. 3: It could be an ID class that stores the ID differently.
Admin
This was probably on an embedded system, where the programmer couldn't rely on having a filesystem.
Admin
You are so hired!
Admin
Surely this can be handled with LINQ.
Admin
It depends on the language and how they're implemented. Generally speaking, I use a foreach when dealing with hashes and fors when dealing with arrays.
I think PHP's manual used to recommend using for loops since foreach was a little bit slower for large arrays. I can't find proof of this, but that's what memory tells me.
Admin
Admin
I somewhat sympathize with the choice to assign the loop index a "too high" value rather than using 'break'. The 'break' statement can create a trap for a maintainer. If he later adds a loop, the 'break' will break out of his inner loop rather than the outer loop, as intended.
Consider, hypothetically, if each row is later changed to contain an array of values, a match on any one of which would select that row. Someone quickly modifying all instances of accesses to the row number to iterate through all values in each row might create a subtle bug where the break doesn't prevent processing of subsequent rows. (Instead it would only break out of the new inner loop and stop checking other numbers for the current row.)
That said, alternative solutions aren't so pretty either. In some cases, 'goto' really is best, but obviously that is pretty ugly. Wrapping the loop into a function that can 'return' is sometimes suitable, but also sometimes completely inappropriate.
I sometimes wish C had a way to name a scope and 'break' or 'continue' a named scope.
Admin
Admin
How would you react if the code was like this?
int i = 0;
while((dtModules.Rows[i]["id"].ToString() != ID.ToString()) && (i < dtModules.Rows.Count)) { ++i; }
dtModules.Rows.RemoveAt(i);
No for or foreach! Crazy.
If this was recent c# you wouldn't need a for or foreach (with linq, etc.), but I guess the dev might not have realised that.
Admin
Real coders use goto