| « Prev | Page 1 | Page 2 | Page 3 | Next » |
|
I'd like to point out (although someone almost certainly has by now,
for(int i=0;i<3;i++)
|
|
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.
|
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. |
Ahem..... DoLotsOfStuff("C:\asdf"); // : void DoLotsOfStuff(string strFolder)
|
To a large extent, labels, continue and break can be used to fake gotos in Java. |
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. |
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? |
|
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.
|
Re: Good or Bad Loopy->Switch..?
2005-09-06 09:10
•
by
dhromed
|
|
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. |
Re: Good or Bad Loopy->Switch..?
2005-09-06 09:20
•
by
dhromed
|
|
OH the shame in not noticing a third page.
|
Because you didn't include the default case. |
|
I shit on your abject idiocy.
|
Re: Good or Bad Loopy->Switch..?
2005-11-02 08:27
•
by
emptyset
|
"thirth". |
|
that would be better written:
|
|
Poop, it orgot my quote:
|
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. |
|
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... |
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 |
|
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... -_-
|
|
Out of runes of magic gold? Need it in urgent? Yes, I can understand you. As the most important currency, without rom gold, you ever can’t do anything. So you need to buy rom gold from those most professional and loyal game online shops with years’ experience and have a good reputation among players. Is there any difficult? No, when you need the rom gold, please feel free to contact us, we are promising to offer you the cheap runes of magic gold with fastest delivery. Moreover, we are online 24/7, you can contact us any time with any question about. So why are you still irresolute? Come here to grab your cheap runes of magic gold now. Crazy about running warhammer gold? Yup, it is so crucial indeed for us in Warhammer Online. Without it, we can even do nothing, without money to buy items, weapons and so on. So enough warhammer gold is substantial.
|
Re: Switched on Loops
2011-02-01 17:41
•
by
Anonymous
(unregistered)
|
|
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
|
|
|
| « Prev | Page 1 | Page 2 | Page 3 | Next » |