- 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'd like to point out (although someone almost certainly has by now, and I apologize if I'm not the first, but I don't have time to read 100 replies) that the Loop/Switch is not *always* a bad thing - it's just when the Switch occupies the entire loop body that it's ridiculously stupid, and in fact far worse than just writing out all the code sequentially.
For example:
Of course, when you use a loop/switch to test 255 different things, and each time it's just a single function call, then it's generally a WHOLE lot faster to just write out the function calls without any loop or switch.
Admin
We had an contract worker who put this kind of for-switch crap code in one of our projects. We fired him. The sad thing is, it was one of his more understandable pieces of code.
Admin
Nope, that's still a bad thing. There do exist valid switches in valid loops, but this ain't one of them. If you switch on your index variable, your logic is screwed up.
Admin
Ahem.....
DoLotsOfStuff("C:\asdf");
DoLotsOfStuff("'C:\Windows\Temp");
DoLotsOfStuff("D:\v2");
// :
// :
void DoLotsOfStuff(string strFolder)
{
// do lots of stuff in the folder...
}
Admin
To a large extent, labels, continue and break can be used to fake gotos in Java.
Admin
Well, you should be. Traditional ANSI C doesn't even have loop-level scoping, as far as I remember, and even if it did it wouldn't do it just cause it felt like it. You can do this in Java too; it's often used as an ugly hack by people who're scared of the break statement. There's nothing magic about the iterator.
Admin
Sorry? Are you MAD? When a plane's guidance software crashes it (the famous example being that Airbus that decided to land itself during an air show) does the programmer sit back and say "ah, but I'm payed a lot and happy"?! No. So why should other incompetents?
Admin
Joodie,
I have seen this 'pattern' in a product everywhere. Initially I thought it was some kind of mistake but turned out to be some kind of 'pattern' used everywhere. Your code snippet is MILES better than the one I encountered.
The for-loop was replaced with a while() to test the i for the end state value. In between the case are littered with 'inline' code (instead of function they dump the code there directly - save the trouble of passing variables and can share the auto-variables[:'(]).
I have been chastised for not recognising that i=9 or i=19 is not goto but controlled-goto. Damn must be reading not the right kind of book.
You have not see the worst yet. There is a situation when there is a next_i value so that after the switch has been executed it is 'popped' off to i.
Imagine the code spanned something like 300+ lines littered with a few dozen of variables declared in C-style in C++ program. How could anyone follow that. To the silicon brain, it is all 1's and 0's and couldn't careless. To a human, this is bad.
That is enough to drive any sane man/woman to jump off Golden Gate Bridge.
Having function between case is a luxury.
Admin
Once when I was 10 or 11 or something, I devised an algorithm to convert Dutch big numbers' zero-counts into words, and back.
Dutch big number names are quirky. In English, you have Mi, Bi, Tri, etc. In Dutch, you do that, AND alternate between -illion and -illiard, so you get:
million
milliard
billion
billiard
trillion
trilliard
etc.
My algorithm involved a division by 3 to get to the Latin number, and even/odd comparisin to get the on/ard. Great fun!
Ah, to be 10 again.
Admin
OH the shame in not noticing a third page.
Admin
Because you didn't include the default case.
Admin
I shit on your abject idiocy.
Admin
<FONT face="Courier New" size=2>"thirth".</FONT>
Admin
that would be better written:
Admin
Poop, it orgot my quote:
Admin
While-case isn't a particular problem, it's a reasonable way to hard-code state machines. Once you know it's a state machine, it's easy to reconstruct the automaton and figure out what the code does. Good practice would include a comment saying "this is a state machine", of course.
For-case is a bad way to simulate line numbers and goto in languages that don't have them.
Admin
Hmm...
I don't want to act smart, but about B's code piece - in some cases the EXACT code might be quite a good idea. When? In parallel programming.
The compilers and operationg systems have several methods to make code parallel and allow it to be splitted into more than one processor. One of the most common is splitting iterations of loop commands into several processors.
The form it's written is kind of strange and I really doubt it's what author meant, BUT - there is a possibility that every function can be executed simultaneously regardless the order (it is unlikely though) - then such a code might be the easiest way to be run parallel.
As I mentioned - not very likely though. Well - WTF...
Admin
The case 1: break; is the only thing that makes sense there. It's so the compiler will use a tableswitch opcode instead of the slower lookupswitch. :)
-S
Admin
LOL. I've actually seen something very similar to exhibit A in one of my coworker's code before! (He even forgot to add the "break" line and he was wondering why there's a bug in his code.) He left the company after two months saying he's going to start up his own company... -_-
Admin
you're joking, right? multithreading initialization for (what should be) constants? and case 1: break; would have no effect on multithreading. plus, doing it the way mentioned here makes it un-multithreadable
Admin
pharmacie en ligne france fiable http://kamagraenligne.com/# pharmacie en ligne fiable