- 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
Actually, when comparing the assembler output from gcc with optimization those lines of code produce the same output as the "else-if" version.
However, the "switch-case-version", gets worse.
Admin
What if mainType (your baseType) is not defined in the map? Whoops! Your clever solution failed. Maybe the original coder came from a python background.
Admin
Admin
We all know here that the best way to write that code was something along the lines of an asm block calculating the offset to jump to by using maintype and cleverly placing the code to execute at the proper place, thus making any execution only take 2 branches, ala: __asm { offset = 11 - mainType; jump to here + offset + 1; subType = 9; // offset 0 (11 - 11) jump to OUT; subType = 6; // offset 2 (11 - 9) jump to OUT; subType = 4; // offset 4 (11 - 7) OUT: }; QED;
Admin
Admin
I have to deal with a lot of this kind of thing:
subType = (mainType == 7 || mainType == 9) ? (mainType - 3) : (mainType == 11) ? (mainType - 2) : mainType;
Admin
Admin
(To a backdrop of country music)
I was clever....when clever wasn't coooool.
Admin
this should have been:
do { blablabla-with-some-breaks; } while (false);
which does not require the extraneous "break", and works as expected. Actually this is widely used.
Admin
Isn't the real WTF that this code even exists? Granted we don't know the meaning of mainType and subType, but if subType is slaved to mainType, at least in these few instances, why even bother with the extra variable? If you want to check that subType=4, why not just check that mainType==7 without all this witchcraft?
Admin
I personally like:
int tmp = ((mainType - 1) / 2); if (tmp >= 3 and tmp <= 5) { subType = mainType - (3 - ((int) (tmp - 3) / 2)); }
Admin
subType = {7: 4, 9: 6, 11: 9, }[mainType]
Admin
Or mainType could be of a user-defined type with a custom == operator. I mean, isn't that obvious?
Admin
The original coder was Billy Ray Cyrus.
Hit him with a switch, in case he tries it again. . . else we'll have to keep putting up with broken code at the heart of the routine.
Admin
You guys know nothing about C++. You don't need a fractured space-time continuum to get x == 7 AND x == 9.
Thanks C++ for saving the universe!
Admin
Admin
Admin
C# case statements don't fall through. You must have a flow control statement at the end of each case. See the C# 3.0 language specification §8.7.2.
Admin
Admin
I'm glad someone else noticed this WTFy comment.
Actually C# case statements do fall through if they have nothing in them, which is handy. IMO C# has perfect switch statements.
Admin
Admin
He handled that case with "if baseType not in types ...".
Admin
Admin
Why not:
Admin
I disagree ! It's a explicit application of the Bohm-Jacopini theorem, that say that any program can be constructed with only two formation rules (IF and WHILE).
See http://en.wikipedia.org/wiki/Bohm-Jacopini_theorem
Admin
Oh, I see.
Admin
Funny how those built-in C++ operators work.
Admin
Ah, but (x == 7 && x == 9) would evaluate to true.
Admin
do { blablabla-with-some-breaks; } while (false); is a terrible way to write { blablabla-with-some-breaks; } .
Admin
CMOS AND and OR gates may be the same number of transistors, but they're totally unsuitable for digital logic applications.
NMOS transistors pass strong 0s and weak 1s (PMOS is the opposite), so with static CMOS gates, you always have NMOS connected to ground and PMOS connected to Vdd (+5V).
If you have a non-inverting CMOS gate, your signal is degraded by the threshold voltage (usually 0.7V) and you'll have to pass it through a buffer (two inverters) to recover a strong signal.
Admin
Anything to make a turd.
Admin
bool breakout = false; bool loopstarted = false; setbreakout: breakout = true; if (loopstarted) goto inloop; startloop: loopstarted = true; breakout = false; while(!breakout) { if(mainType == 7) { subType = 4; goto setbreakout; }
}
captcha - haero: Me for making such awesome improvements to some already awesome code!
Admin
Or maybe even a switch statement.
Admin
They were cheaper because the typical AND gate was actually a NAND gate which fed both inputs of a second NAND gate to invert the output.
I also recall converting circuits into all NORs also.
Admin
Good point. Clearly the correct solution is:
Actually, I see a lot of code like that every day. I just saw a program the other day filled with statements of the form:
i.e. they repeatedly checked that the value was not an empty string before comparing it to the string they actually wanted. Just in case, I suppose, it might be both "" and "X" at the same time.
Admin
because you only see a small snippet here.
what if
crap code that sets subType, based on mainType; . . . more crap code that modifies mainType; . . . even more crap code that examines subType (no guarantee that mainType is even remotely related to subType here);
Admin
"a profession where proper understanding is the exception" - I've just been waiting for the chance to use the word 'oxymoron' and I think this might be it!
Admin
The definition of the PLUS operator is an exercise left to the reader.
Admin
But that does not explain why 9 is a subtype of 11.
Admin
This is awesome.
Compromise solution:
Admin
So, I've never seen something this ridiculous, but I've heard that in some government run software projects, removing code takes more paperwork than writing new code. So maybe, just maybe, this guy just didn't want to fill out the "removing while loop" form.
Admin
Argh, self .. FAIL!
subType = mainType + offset;
Admin
Reminds me of when i poked around in the source code of VICE and found that in a lot of places they had wrapped code in:
when probably only the curly thingies would be enough...
(2nd try)
Admin
I can see nothing wrong with that code.
Admin
with all the talk of switch and if.. else, a simpler solution has been ignored, the goto of course
if(mainType == 7) { subType = 4; goto end; }
end:
Admin
subType = (int)(0.125 * mainType * mainType - mainType + 4.875)
Admin
If this code:
do { /code here/ } while (0);
was used in a #define, than considering the while(0) to be boilerplate doesn't bother me. Easier than trying to remember the rules of when you do and do not need it.
It was inlined code ... then maybe someone just had a finger-macro set up they couldn't ignore.
Admin
Okay, sure, it's not super elegant. But it works just as well as a case statement, even if it causes a more severe double-take. This is not Worse Than Failure - it's not even Failure. This is Shoddy Success.
Yes, there are other ways to do it. Turing-completeness is like that. Deal.
Admin
Yes, everything is different in Russia, err... in embedded. MISRA has guidelines for embedded code which forbid use of goto. So guys work around about it, as accidentally explained today in a blog http://embeddedgurus.net/stack-overflow/2010/02/goto-heresy.html
Admin
Within integer domain:
subType = (mainType * mainType - mainType * 8 + 39) / 8;
And smart compilers will know to use shifts for * 8 and / 8.