- 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
Well on embedded systems without a filesystem, etc., etc....
Admin
Maybe the developer didn't know about else-if, but apparently, Rob (the submitter) didn't know about the switch statement either.
Admin
Yeah, this code sucks. He should have used do { ... } where (false) so he didn't have to break at the end!
CAPTCHA: nulla - stereotypical Italian undefined value.
Admin
He also seems to have no clue about the C++ switch statement....
Admin
I'm not familiar with C++, but wouldn't a switch() be better in this case than an if-elseif, because you're testing the same variable?
(3rd attempt... when will the human finally look into this?)
Edit: I've cracked it: The errors are there to give the ninjas the time to do their thing!
Admin
What's wrong with a swtich/case construct?
Admin
Wow. Just, wow.
Admin
Any procedural solution for what this code is attempting would be smelly. However, I must say this is probably one of the ugliest ways to do it.
Admin
Don't know if it was mentioned already, but what about switch?
Admin
Else if and switch sound good, but what are you going to do when mainType == 7 AND mainType == 9, huh?
Admin
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.
It could be written:
There's no potential overlap so no NEED for an else (even if it might make it more readable).
Admin
I tend to avoid switch constructs myself, unless each case ends with a return statement (e.g. the switch is the main part of some conversion function). The reason being that I dislike having breaks in each case, as well as avoiding potential logic bugs that can arise due to forgetting one.
Admin
But what if mainType is 7, 9 AND 11? Then you're just screwed!
CAPTCH: dignissim (missingID backwards, perhaps?)
Admin
Why can you mortals not accept this highly optimized piece of code? Everybody knows switch statements are a CPU killer, even worse than an if...else construct. This sub contractor really knew what he was doing, although I expect he would have rather written it in a proper language like assembly to begin with.
CAPTCHA - Genitus - somebody with highly intellectual genitals?
Admin
Admin
Admin
If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.
Admin
I think you're right, he probably had no idea about the 'else' clause. Seems unlikely but then it was only the other day that we had someone who didn't know about 'break'. The simple fact is that we're working in a profession where proper understanding is the exception, not the rule.
Admin
if(mainType == 11) subType = 9; if(mainType == 9) subType = 6; if(mainType == 7) subType = 4;
fixed
Admin
Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.
Admin
Sorry, you threw me off by posting your captcha, which usually is an indicator the post will not be funny.
Admin
Point taken, sarcastic response retracted ;)
Admin
Admin
Admin
It's been a while since my Computer Engineering classes but from what I remember the reason for using NAND gates instead of OR/AND gates was because they had less transistors making them cheaper. This also meant their propagation delay was lower and thus faster than an equivalent AND/OR circuit.
I vaguely recall exercises in converting AND/OR circuits into only NAND gates.
Admin
Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to
True, the original code does not alter the value of the subType if mainType is outside the 7,9,11 range. Let's add another twist, assuming that mainType is an integer
This is in my mind way more legible and does exactly the same. Or I'm completely confused?
If the code was about mapping arbitrary mainType values to subType values, then a lookup table would be better, by the way.
Admin
"so he cobbled together an 'else if' in the most ridiculous way possible."
That sounds like a challenge. I'm sure someone here could do more ridiculous!
Admin
Actually, the professor is right about the NAND gates (and the poster should have paid more attention in class).
It IS the most basic 2-input-gate to use and all other gates are designed from NAND-gates (using only 4 transistors) and then the redundant transistors are removed for optimization.
Even an AND-gate is just a NAND-gate with and inverter (2 transistors) in any processor design you'll find.
Check this wiki for further info: http://en.wikipedia.org/wiki/NAND_logic
Admin
No need for threads or alternative universes:
#include <iostream>
#define P(_X) std::cerr << _X << std::endl;
class MainType { public: bool operator==(int) { return true; } };
int main() { MainType mainType;
}
Admin
break;
/* Wow, that's a sexy piece of code! It looks so beautiful I want to slip something in it's drink and take it back to mine, where it'll wake up in the morning confused and with a sore rear! */
continue;
Admin
I think without the knowledge of else if, this isn't the most ridiculous way to cobble the semantics of an else if statement together. I think it's pretty damn elegant actually...
Admin
"A human will eventually look at it." riiight.
Admin
People who think this is a comment on thread synchronization are TRWTF. Clearly we're talking about quantum computing here.
Admin
Yes, a switch makes more sense, but, given that the original programmer knows about "if" how is it conceivable that they don't know about "else" and "else if"? You could (almost) forgive that they just plain didn't know about switch, but to know "if" and not "else"/"else if" is just mind blowing.
Admin
Haven't you ever seen .Net-optimized code before?
Admin
It's the infinite loop that always breaks after the first execution. That should get patented immediately.
Admin
As an ex-VB programmer, this is the one time I miss VB. Select Case ... Case ... End Select was a much better construct than switch.
I'd like C# to include a switch statement where each case had to be followed by a block, so it would look like:
select (mainType) { case (7) { subType = 4; } case (9) { subType = 6; } case (11) { subType = 9; } }
I don't have a problem with fall-through switch, but can we have a non-fall-through version where you can't mess yourself up by forgetting a break?
I've used the select keyword in my example because that's what VB uses - if C# can steal good ideas from non-Microsoft languages, why not one from their own VB?
Admin
Admin
Everyone here who's talking about "not knowing about switch statements" is missings the bigger picture. Look at that code again. That's very close to a switch statement. Clearly he knows about switch statements -- I'm guessing from VB --- But doesn't realize that C/C++ has them also.
I'll bet he's thinking that he's created a way to "do select/case in C++", and is feeling very proud of himself....
Admin
using else if gives you the benefit of not checking the conditionals after the first match has been found.
if(mainType == 7) subType = 4; else if(mainType == 9) subType = 6; else if(mainType == 11) subType = 9;
This should be a bit more efficient then -- disregarding the fact that switch would be even better.
Admin
Pfft. You C guys and your silly switch statements. All we really need is a map!
Python FTW!
(of course it'd be better to avoid the magic numbers altogether, but we don't seem to have that luxury here)
Admin
subType would be 4, just like in the original code.
Admin
Admin
This is a stupid block of code. They obviously should have used GOTOs.
Admin
frawress
Admin
Other than the use a switch and you don't even need an else statement comments which are obviously true. I'm guessing that code has had some bad refactoring done to it. I'm guessing a giant method operating on a while loop for multiple records was used initially and broken into sub methods (badly).
CAPTCHA: jumentum
Admin
Nobody here seems to know about a map or an associative array.
Admin
Who'd've thunk!
Admin
That's assuming a mainType always changes the subType. What if the mainType is not 7, 9 or 11.
Admin
That's assuming a mainType always changes the subType. What if the mainType is not 7, 9 or 11?
First reply went wrong.