- 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 think that's great. I'd remove the extraneous {}'s and have an efficient enough solution in 3 lines.
Admin
And that would eliminate the need to maintain the goto labels. It would also permit a clever editor to identify, (by name), what scope closing braces or 'while' clauses were associated with.
But your point is definitely valid. Named scopes really are a very slightly more elegant way of providing the same capability as 'goto'.
Admin
IMHO, this:
plus this: = the answerAdmin
Admin
TRWTF is that he did not use break
Admin
Apparently the origional coder don't think it's worthWHILE to use WHILE loop.
Admin
Too verbose. How about
int i = 0; while(((dtModules.Rows[i]["id"].ToString() != ID.ToString()) || 34598 == dtModules.Rows.RemoveAt(i)) && ( dtModules.Rows.Count > ++i) );
This assumes that dtModules.Rows.RemoveAt(i) returns an integer != 34598; if return type of dtModules differs, code must be adjusted accordingly.
Admin
Clearly this coders boss does a project wide search for such evil constructs as break, and goto, and absolutely forbids them. The coder disagreed with it and decided to protest by leaving a randomly failing piece of code.
Admin
The comment after the assign of i to 34598 is the real WTF.
The algorithm is to remove the first matching record in the first 34598 records, and then any matching records after that point.
Admin
It's not a clean quote, but since it is my own old post:
In case dtModules.Rows.RemoveAt(i) doesn't have a return value, a wrapper is required, extending the code to three lines. This could be used anyway to avoid the bother to lookup the return type, but it's less fun: The thrill of the possibility that the funtion might actually return 34598 and thus lead to more interesting behaviour would be missed. Then again is still an option...PS: There is one fail in this updated code: The Preprocessor will not actually insert the comments at the right place.
Admin
but WHY, use break;
Admin
There are no right or wrong way. You'll have to choose your path my friend. You can be a coding GURU, hacker, or whatever path fits you.
In this case he chooses to be a computer "Wizzard", and as you may know: Wizzards use magic, a lot of it!
Admin
Your sarcasm > my sarcasm. I think...
Admin
Admin
Undefined's hit rate of 1/6 is shamefully poor though.
Admin
When removing an item, also use ‘--i’.
Or change the loop:
Admin
… hmm, of course, there's the “jump out of the loop” bit. And somebody else asking what if more than one item matches. Oh well, more fail :-)
Admin
[quote user="Undefined"]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.[/quote]Except when you need to.
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.[/quote]The key to this is knowing when to break these rules.
Admin
Previous revision in source control:
Admin
Sure it's a WTF, probably RUSHED attempting to fill more than one hat DBA, NETWORK ADMIN, DEVELOPER, etc and just does not know! Also he applied for an HR position and told someone he had done some Access progamming, could be great code depending on the circumstances.
Or maybe he's a frigging idiot?
Admin
Consider someone maintaining this code.
If he changes the inner 'for' loop into a 'goto' loop for some reason (or the inner loop is no longer needed for other reasons), the 'break' will now break out of the outer loop. Ack! Obviously, that's not very likely if the code all fits on the screen.
Also, if he later adds another loop, the 'break' may then break out of the new loop rather than the old one. Even if the code fits on the screen and this is obvious, he may have to unnecessarily re-architect my code to make his changes.
In fact, that's probably the real reason I like the idea of named scopes and break/continue taking a named scope.
Admin
if you mean "while" use "while"
Admin
Wizzards spelt that way just see their babies jive with angel fingers whilst wishing it could be christmas everyday.
Admin
A joke where you can bill $100+ an hour without a college degree, Mr. I Have All Kinds Of Rules
Admin
I with the WTFs I dealt with in my job were anywhere near this benign.
Admin
Admin
FTFY
Admin
This actually defeats the loop optimization, so "break" is preferable.
Admin
Using foreach on a loop that does a RemoveAt on the same data structure: FAIL
Admin
Umm, Exceptions are REALLY inefficient. You are going to throw one every time you remove an item? This is TRWTF.
Admin
TRWTF is that you have NO IDEA why GOTO is evil. How old are you? 15?
GOTO is evil because you have to search the entire code for the corresponding LABEL and people do all kinds of wacky things like jumping into the middle of a FOR LOOP.
Going to the end of the current structure is hardly evil. It is simple and makes perfect sense to everyone reading.
Admin
Wow. You're almost smart.
Admin
L10: int i = 0; L20: if (i >= dtModules.Rows.Count) goto L70; L30: if (dtModules.Rows[i]["id"].ToString() == ID.ToString()) { L40: dtModules.Rows.RemoveAt(i); } L50: i = i + 1; L60: goto L20; L70: return;
<Aside>Never noticed C# doesn't allow numeric-only labels before...</Aside>Admin
oh, for the love of (the non-existent) God! that's the SAME thing wow but as a joke... still fail.
Admin
Well, you can argue that every control structure, even functions, are a goto with another name. However, named break/continue scopes have several advantages over a goto:
They don't require the label statements, thus reducing verbosity and eliminating the risk that new code will erroneously be placed on the wrong side of the label.
They don't require you to search for the corresponding label to see what the code structure does. It's obvious (both to man and machine) from 'break' that it breaks out of a loop and from 'continue' that it continues it.
Naming the scope has other advantages, for example, the scope can be summarized by its name in a high-level code view and the close brace can be decorated with its name on hover by a clever editor.
A 'goto' label may inhibit optimizations if the compiler can't easily tell all the places it might be referenced. An extra pass through the function may be needed (or not done and optimizations left out) because the compiler assumes the label may be called from anyplace in the function scope. A named scope immediately tells the compiler that it cannot be jumped into or out of from outside that scope. (Unless a goto label appears inside it, of course.) Optimizations that are disabled by a goto label need not be disabled by naming a scope.
Named scopes are easier to learn how to use correctly than 'goto' is. Many programmers who have no clue when or how to properly use 'goto' know how to use 'break' and 'continue' just fine and could (and maybe even would) much more easily learn to use named scopes.
Many programmers who consider even clever use of 'goto' as a kind of personal failure would likely see clever use of named scopes as a kind of personal victory. (Okay, I'm joking about this one.)
But I agree that none of these factors, alone or in combination, is anything close to Earth shattering.
Admin
Wait, just wait a second! The loop-breaking thing is nowhere close to TRWTF. Using the loop at all it TRWTF.
This is what the code should have looked like:
DataRow drModule = dtModules.Select("id = " + ID.ToString())[0]; dtModules.Rows.Remove(drModule);
That's it. No loops, no WTFs.
Admin
In a nutshell, yes.
Admin
Reassuringly, resharper was going nuts at me while I was proofing that joke...
Admin
Also, commit FAIL...
<< L40: dtModules.Rows.RemoveAt(i); }
Admin
Implies that you are covering the whole group, from 1 to 10.
"While" loops imply that you are going to repeat until a condition is met.
What you are advocating is a coding convention like the One True Brace Style, explicitly contradicted by the clear language design, but sanctioned by your friends and your habit.
Admin
TRWTF is that have NO IDEA why GOTO is evil. How old are you? 15?
GOTO is evil because you have not idea how you got TO a particular label, or what the STATE is when you ARRIVE at a particular label. Finding a jump target in the code is trivial, and always has been.
By the way, going to the end of the current structure is explicitly what "Structured Programming" was meant to replace, and was certainly included in the idea that "GOTO considered evil". This was a contentious idea: many people at the time considered that going to the end of the current structure was hardly evil. To them, it was simple and made perfect sense.
Admin
Because the developer queried all of the current users, and didn't find any who had 34599 modules. So, obviously, the number didn't need to be that high.
Either that, or the developer tried that, and it segfaulted. One less worked. ;)
Admin
Admin
If the Count Property is a short than this code make prefect sence
Admin
Erm... doContinue is false and always false. This code does nothing, now thats elegant!
Admin
We actually had coding guidelines banning break in such context (only to be used with switch...case ant there mandatory)... This is who those WTFs are born.
Captcha: bene... Well everthings gona be bene.
Admin
Off by two. And no, sarcasm does not excuse an off-by-two error!!!!1!1!!1
Admin
Weird how it took so long. But maybe dtModules is actually short for "damn tasty modules"
Admin
you can modify in a foreach if you use it as a delegate
objectList.foreach(delegate(....
Admin
while(dtModules.Rows[i]["id"].ToString() != ID.ToString()) { i++; } dtModules.Rows.RemoveAt(i);