- 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
Regarding your first point.
Last time I checked you couldn't use a foreach loop if you modify the collection within the loop's body (like say by removing an object from it).
Admin
Admin
Admin
Admin
This code won't work. It needs to go through the rows collection backwards. Removing item 0 shifts the indexes of all of the other items.
Admin
Admin
34,598 modules should be enough for anybody.
Admin
Both FOR and FOREACH loops are sequential scans. The real issue is why the code didn't use a more efficient look-up method for the "modules" data set.
This WTF only really matters when there is a large "modules" set to process. In that case, the high-number stop can collide with a real cell. Otherwise, this code sadly satisfies the BREAK requirement.
Admin
Oh, yeah, like this (pseudo code) - this avoids any potential concurrent modification exception and pushes the logic to the db, so any bottlenecks aren't our fault.
Admin
The fact that this is even allowed is weird: normally a compiler should check for this, and not allow other statements to change the loop control variable!
Admin
I agree that using a magic number to do this is weird, but there is really a big difference between "I want to exit this loop NOW" and "I want this to be the last iteration through this loop."
In this example, there was nothing else to be done, but in another situation there may have been other processing within the loop that still needs to happen, even though we want this to be the last pass through the loop.
Using this pattern (in a less moronic way) is not a WTF in and of itself.
Admin
The code's C#: dt is the Hungarian notation prefix for a DataTable.
Admin
There's no reason to iterate in the first place. Surely there is a function (either in the library or part of the collection's class) that can find the key of the first element of a collection that causes a given predicate to evaluate to true.
Admin
A truly innovative programmer would have used FILENAME_MAX instead of a random constant.
Admin
Admin
Well, actually you can't modify a collection if you use it with foreach, so the for-loop is completely valid.
delenit: a hermit with delete button
Admin
You're absolutely correct. Good thing I was a CS + music theory double major, not English.
Also, wow @ teh hostility.
Admin
tl;dr: Stoopid troll is stoopid.
Admin
Beautiful. Notice that in all of my posts I never resorted to namecalling.
Addendum (2010-01-27 14:03): Thankfully most posts here are logical, otherwise I might think that trwtf is the coding community... open discussion on optimal programming technique is terrific, namecalling over grammar (or anything, really) is a bit sad.
Admin
This is so obviously a clear case of a manager deciding that break was a form of goto, and therefore was forbidden.
Admin
I see it do you!
34,598
well 4+5 is 9 and another 9 is 18 and then if you add 8 its 26.. then if i subtract 3..
23!
Admin
But why 34598? Why not 34599?
Admin
Now consider a modification where the loop index is used after the deletion. Maybe to generate a message to the user: "Row {rownumber} was deleted." It will always tell you that row 34598 was deleted!
Basically you cannot defend your code from random future changes.
It does. You can put a label where you want the scope to end, and then you use "breakto label;" when you want to break out of the scope. Try it!
Hans
PS. Don't forget to stick a "#define breakto goto" somewhere ;-)
Admin
"wow, that’s more wrong than I could have ever imagined" It's not a goto. It's not continuing to iterate through the remaining items after it finishes. It's not self-modifying code. You clearly don't have much of an imagination!
Admin
For a more reliable version replace the assignment:
or perhaps even:
Admin
Admin
This is even better:
try { for (int i = 0; i < dtModules.Rows.Count; i++) { if (dtModules.Rows[i]["id"].ToString() == ID.ToString()) { dtModules.Rows.RemoveAt(i); throw 1/0; } } } catch(...) { //used to jump out of the for }
Admin
I still like mine completely without for loop best. :)
Admin
What boggles my mind is that he clearly knew how to get the actual number of rows, like he did in the first line. So what line of thought led him to that monstrosity, instead of doing at least
Captcha: consequat ... this practically begs for some kind of witty pun, but I can't think of one. Oh bummer.
Admin
IMO the better way to do it is to use a state variable. Not sure of the C# syntax (if that is what this is), but if it follows C:
Admin
FTFY. Actually, it does look sort of offensive. No wonder he didn't opt for break.
Admin
Also, wow @ teh little emo sensie that found his way to the TDWTF.
You've really got to get that sand out of your vagina; it's making you cranky. Does it itch?
Admin
But removing the item during a foreach iteration is usually bad form... the for loop is fine, as a foreach won't give you the index of the item to remove. However break; or i=Integer.MaxValue or something similar is better than an arbitrary magic number.
Admin
'goto' will do exactly that. It's silly to add a feature to the language that does not reduce verbosity merely because you want to avoid using the word 'goto'.
Consider:
How would "named scopes" be less verbose?
Admin
Real programmers program on the bare metal with jumps and self-modifying code. ReadThe Story of Mel - you'll get the idea.
Admin
Should just do it like this:
Admin
Or better like this - avoids the magic number and works with large vectors:
Admin
This is the best joke I've seen since that one about how to completely bamboozle someone; to wit:
Admin
The value of i is used later in the function to look up a bank account to transfer funds into.
The coder's personal bank account number is in row 34598.
The comment is a diversion.
The coder is a genius.
Admin
Admin
Sure they did, you can write anything and call the method toString(). In fact, I would love to see the toString() method they wrote, I am sure it is a hoot.
=)
Admin
for (int i = 0; i < dtModules.Rows.Count; i++) { if (dtModules.Rows[i]["id"].ToString() == ID.ToString()) { dtModules.Rows.RemoveAt(i); goto i34598;
} }
:i34598 //used to jump out of the for
even better (although I did read somewhere that it may be something something)
Admin
Im sure some one else has pointed this out by now.. but.. foreach would throw and exception if you removed an item from the collection that was being looped through.
Admin
[quote user="eViLegion"][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. [/quote]
Damnit..
Admin
Use a FOR loop when you want the loop to execute a known number of times.
Don't fiddle with the FOR loop iterator variable.
Use a (DO) WHILE loop when you want the loop to execute an unknown number of times.
Don't put logical && operators inside a FOR loop definition.
Don't use empty exception handlers.
Avoid goto unless you know what you are doing.
Admin
Double Damnit..
Admin
All your rules can suck it.
Admin
This is exactly why Software Engineering is seen as a joke to the rest of the engineering world. I hope to God that you never find yourself working on something important, Mr All-Your-Rules-Can-Suck-it.
Admin
Oh sorry, no I'm mistaken, that was a "fully fledged" C compiler that didn't even have malloc, while this is C++/Java/C#...
Admin
Yeah, I see the mistake. He should have use int.MaxValue.
/sarcasm