- 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
NANDs are actually cheaper in hardware than NORs, even though they use the same number of transistors, assuming you want a gate with balanced rise/fall times (you usually do).
PMOS transistors are about 2x-3x as large as NMOS transistors, and a NAND allows you to use two double-width NMOS and two normal-width PMOS, whereas a NOR requires the PMOS to be double width and the NMOS normal.
I didn't explain this very well, but I swear it's at least mostly true.
Admin
if(mainType == 7) { subType = 4; goto subtypingDone; } if(mainType == 9) { subType = 6; goto subtypingDone; } if(mainType == 11) { subType = 9; goto subtypingDone; } subtypingDone: /* Some more code */
Admin
Meh, I'ma have to start adding some
while(true){ ... break; }
into my code, just to screw with people ^^
Admin
Admin
If NAND gates are cheaper and/or faster, but if you havce to use more of them to implement the other curcuits, isn't it a tradeoff? And the AND or OR gates might be cheaper overall than using NAND gates with other hardware?
Admin
Everyone and their mother knows that else if and even switch are way less efficient than a while loop with ifs and breaks. Come on guys, RTFM.
Admin
Go lookup the history of the "NORDIC" computer.
An early computer, made up of just NOR gates.
Admin
Admin
You know, that looks a lot like the Perl code to emulate a 'CASE' statement, in fact it looks almist identical to the sample code in the Camel. The "switch" builtin is planned for Perl 6.
It also looks very vaguly like the old Applesoft Basic way to do a case statement...
Admin
Schrodinger's integers collapse to a fixed state during the test in the first "if" and so this logic would work fine for them.
I think you meant "Heisenberg's integers", which change to a new value every time you test them.
Admin
This is clearly an attempt to make a simple task look more complex (by slightly longer, further indented code).
Any manager would instantly recognise that this was no trivial task and that the contractor was worth the 6 months he charged to nut out so difficult a problem....
Admin
I don't want to be mean, but aren't most logic gates actually implemented using NAND gates? I think they're actually the cheapest option for implementing most binary logic, at least that's how it was with TTL technology.
Admin
I think you'll find that landing in an alternate universe actually is a major thread sync issue.
Admin
Wait! Maybe, because this is C++, the variable mainType is an object which has the comparison operator overloaded, i.e. operator==(int), with side effects. So when you compare the mainType == 7 it actually modifies the mainType to 9. Thus the second comparison is executed.
Additionally, the assignment operator for the object which subType is an instance of has been overloaded as well.
Bottom line, this could be the most devious piece of code we have ever seen! It isn't a WTF; it is a backdoor into the application! The contractor is actually a world class hacker out to undermine your company for not renewing his contract!
{lmfao}
Admin
Congratulations, you're TRWTF.
Admin
HA! only a coward would use a switch/if else on a volatile variable ^^
Admin
Why not use mainType right away since it maps in a unique way to subType?
Admin
You started out with a full TopCod3r treatment on causation and correlation, but you slipped up at the end there with the cromulent bit about 1st order lookup... still, full marks!
Admin
I'd suspect you of trolling, if it wasn't for use of the term TRWTF, which usually indicates lack of savvy. Therefore I'll indulge this question. Most branching can be eliminated in OOP languages through use of polymorphism. Any procedural approach, including a hashtable, requires the original code to be rewritten anytime new "types" are added or existing "types" are changed. Using polymorphism allows new code to be written that fufills the original design contract, but does not require rewriting and possibly breaking existing code.
BTW- this concept is pretty basic OOD.
Admin
BTW why does nobody talk about the FILE_NOT_FOUND option?
Admin
There is no such thing as an 'else if' in the C++ standard. An 'if' statement can have an else clause containing a statement, which could just happen to be another 'if' statement. Likewise, the grammar does not directly address the use of blocks for any of 'if', 'for', 'while' or 'do-while'. Instead, the statement(s) governed by these compound statements could just happen to be block statements. A little known but occassionally useful feature of the language is that you can use block statements independently of the above-mentioned constructs, e.g. for controlling variable lifetime.
(I wrote a C parser once and can't help being a C/C++ wise guy.)
Admin
Admin
Admin
Admin
You sure have put a grin on my face. Thanks :-)
Admin
It looks like he also never heard of 'switch'
Admin
No, it's EVE online!
Admin
What do you mean else... if? Isn't this a switch?
switch(mainType) { case(7): subType = 4; break; case(9): subType = 6; break; case(11): subType = 9; break; default: break; } </ code>
BTW is it possible to indent code in this forum?
Admin
... or if it is a global variable written to by another thread?
Admin
The real WTF is that there's no way to switch this to a threaded view.
Admin
Admin
maybe this is built on some de compiled auto generated code ive seen such soluions in this type of code
Admin
We came across one even worse. Not so much as an 'else' statement in sight.
if (x) ...code...
if (!x) ...code...
Admin
For everyone who thinks switch/case statement are extremely superior to if-else construct (in this situation):
In the olden days (and maybe still) the case/switch statement checks the controlling variable against the lowest value, then checks against the highest value, then does an indexed GOTO using a table <<minor and major implimentation variations, depending upon your particular compiler and hardware>>
The resulting code is longer, slower, and more buggy (yes, I'm an old timer), than the nested if-then-else-if.
As a result most coders only used the case/switch statement for situations where the overhead was small compared to the many if tests. AND, as a result, it was a little confusing to someone who came across a two or three element case/switch statement -- so for maintenance, case/switch statements where there were lots of cases, such as apply productions in a parser.
The "obviousness" of the switch statement vs the cascading if-then-else construct is in the eye of the beholder -- and the particular group you are working for/in.
I would think a case of three would give me more pause than a three level nesting -- in this particular situation.
In the olden days the code space was definitely a consideration. Of course in an embedded system ...
Admin
Admin
Obviously you're being sarcastic here, but a do-while-false isn't necessarily a bad thing--provided there's a lot more in there than just those 3 if statements. If you had many, many, many lines of mostly disjoint validation routines (i.e. else if's wouldn't cut it), it might be useful to be able to use the break to jump to the end once you've reached some condition. It's essentially emulating a limited goto, which could be used properly.
CAPTCHA: genitus. Sounds like some weird STD.
Admin
Admin
Admin
In this year (2010), even the C compilers have been mostly upgraded to optimise if-else constructs and switch constructs as well as you can. If you had many, many, may lies of omstly disjoint validation routines, the compiler will generate a jump to the end once you've reached some condition.
Have a look at the code your compiler generates to see what your compiler thinks is the best code to generate for different ways of writing this.
(I do look at the generated code, because the targets I have don't have file systems)
Admin
frits is right -- throwing a bunch of if/else statements together is usually a hackish way to do things. We don't know if there's a viable way to OO this, but if there is, it would be way better than an ugly if/elseif/elseif/etc/etc.
Admin
Before I found your contribution, I came up with the snippet below. I think it's superior:
Harder to understand is the goal, right?
Admin
Challenge accepted. This must be the best way to do this:
int mainType(int x) { return x&x<<2&8|x>>3&x>>1&1|x<<1&x>>1&4^4|~x&2; }
;D
Admin
errr.....
nowhere does it say what the type is of mainType or subType...
it would be perfectly reasonable in the land of WTF to override the == operator and the = operator so that
== on mainType could == 7 9 and 11 at the same time
and = on subtype could potentially add something to a colllection.... which then can be used with an == to see if its in that collection ;)
operator overloading makes things fun.... heck the operation of == in operator overloading WTF land could accumulate the value also! :)
so most of the rewrites in this thread wouldn't work if that was the case, most are just assuming its an int (which it most likely is, but hey )
Admin
I understand that there is a school of OO thought that states every branch should be replaced by an object. It's a little bit of an understatement to say that not everyone agrees with this degree of object-fetishism.
Admin
Shh. Don't scare the code monkey. All this talk of OO is making him feel threatened.
Admin
Nice run-on sentence. I notice you didn't actually address my reasons for suggesting a switch or other procedural construct is the wrong strategy. I never said I would rewrite anything. I would have never written this way in the first place. Remember: Design first, code second.
I learned OO on the job, not college. Did you go to college? I suspect you wouldn't know how to go about doing this using Polymorphism. You write a base class that implements most of the logic. Then you create simple classes that customize or extend the base class. It's really simple.
Feeling superior and being superior are not always the same thing.
Admin
Admin
Admin
Admin
subType = (mainType==7)?4:(mainType==9)?6:(mainType==11)?9:FileNotFound;