- 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
Though the disadvantage is that, if I understand the GCC ASM syntax correctly (which is far from guaranteed), it constrains the compiler's optimization opportunities by forcing a and b to be in eax and ebx at that instruction.
It's also definitely not portable between compilers or architectures.
Admin
that is why USA need so many greencards... too many noobs and yet proud
Admin
Admin
Admin
This could have been done by a person who thinks in assembler and possibly has a mind corrupted by microoptimization. Translated, he's doing something like:
cmp eax, 0 jeq exit cmp eax, 1 jeq exit cmp eax, 2 jeq exit cmp eax, 3 jeq exit mov eax, 0 exit: ret
A reasonably smart compiler will translate the original C code into something very much like this. For example, If this were (for some weird reason) a performance critical path, the default case (number not one of 0,1,2,3) won't go through any jumps.
Admin
Hey I just tested:
$Bill = (!smart) ? true : false;
and it returned true!
Admin
Of course, since my eyes tend to miss "the obscured '!' operator" late in the day, I'm cool with that code.
Admin
Ooo. Numerics. We should use FORTRAN here, because of the numberes:
100 CONTINUE INDEX = 0 GO TO 500 200 CONTINUE INDEX = 1 GO TO 500 300 CONTINUE INDEX = 2 GO TO 500 400 CONTINUE INDEX = 3 GO TO 500 C comment: goto 500 500 CONTINUE RETURN END
Captcha: smile (at this)
Admin
From a security perspective, he might have done the right thing in that his code does constrain the output to a defined set.
Admin
Admin
I have it on good authority that this bit of code is from the original source code behind the amazing Big Red Button:
http://www.pixelscapes.com/spatulacity/button.htm
Admin
Posix sh. The language that outperforms Java.
Admin
Private Function GetValue(selected as Long) as Long GetValue = iif(selected>3,0,iif(selected<0,0,selected)) End Function
Admin
This is the shortest "swap 2 variables" implementation I was able to come up with.
b^=a^=b,a^=b;
I tested it by gcc -W -Wall -Wextra compiles just fine. (gcc 4.1.2)
Admin
You just broke the application by introducing an integer underflow.
Admin
Of course, the point of this thread is to post the shortest, and the most convoluted, code, so here I go:
Or how about a Java version:
Admin
#define GetMessage(X) (X & 3)
Admin
you are also enginieueieur from India or just cannot code?
Admin
Admin
Admin
"also"? How are things in India, anyway?
Short, sweet, same result as original. Anybody can obfuscate.
Admin
Depends on your compiler, and your platform (longer code might not fit into the caches).
Wrong. More lines of code means that the compiler will need to do more disk I/O, parsing, etc.
Depends on your compiler.
Sort of. If you're programming resource constrained embedded systems (like I am), code size and speed do matter, sometimes (a lot) more than clarity.
Admin
you better go to check C for dummies
Admin
// <param name="selected">a value between 0 and 3 inclusive</param> private int GetMessage(int sequence) { return sequence; }
Admin
sigh ok, I'll byte...WTF are you talking about?? Here's code and output. Macro updated to handle full range of input, staying bitwise for fun
#define GetMessage(X) ((X&0xfffffffc)?0:X&3) main() { int i; for(i=-5; i <= 5; i++) printf("GetMessage(%d)==%d\n", i, GetMessage(i)); }
produces
GetMessage(-5)==0 GetMessage(-4)==0 GetMessage(-3)==0 GetMessage(-2)==0 GetMessage(-1)==0 GetMessage(0)==0 GetMessage(1)==1 GetMessage(2)==2 GetMessage(3)==3 GetMessage(4)==0 GetMessage(5)==0
OMFG! It's correct!
Admin
finally. in meantime until you realized it, India pwned you. And I am forgiving you the fact that you are casting constant to int without knowing its size.
Admin
He's talking about the original code (which IS quoted in the post you replied to...), #define GetMessage(x) (x&3), which maps most numbers outside of the range 0-3 to non-zero numbers and hence is wrong.
You'll notice the code you give below is DIFFERENT than the code being commented on in the post you're replying to, and which was written by you...
Oh, see, you DO realize why the person was continuing to insist you were wrong.
Really?
OMFG! Your macro behaves differently than a function that does the "same' thing!
You know, if you're going to act all smug about your C-l33tness, and say that it's just as valid to use macros as a function, you should at least check and make sure you realize the pitfalls of macros first and try to avoid them.
Admin
I'm constantly amazed at how many people will read this blog, feeling very superior, and then post code that doesn't work.
Wow
Admin
indeed
Admin
Okay, sorry about the tone. Was more indigestion at "enginieueieur" than anything.
You make a good point. I guess I've developed a few blinders from being a one-man shop. I'm the only one that uses my code, and I do know the difference between macros and functions.
Though a few more parens (now it's looking ugly) fix your example, I thought you'd point out the more obvious problem - GetMessage(i++).
You were right. I was wrong.
Admin
I still haven't seen any Postscript here. So, here it goes: :D
Admin
Haskell again (but correct this time):
f x | 0 <= x && x <= 3 = x f _ = 0
Admin
OK, enough fun with rewriting it in the most bizarre manner possible. Seriously, now, with this function being so easy to understand and rewrite into a sensible form, doesn't anyone think it's a pretty big WTF that the consultant's response was "We have to rewrite everything?"
Admittedly, this code may just be the tip of an iceberg, and some of the other problems may be harder to fix that this little gem, but rewriting is far too often the first thing people think of, and rarely the right answer.
Right now I'm working on a major update to some software that has been around for about 5 or 6 years. It has WTFs in it that would blind you or send you fleeing, screaming for mercy. It also has years of domain knowledge baked into it. If we tried to just rewrite it from scratch (as several of the developers have suggested), it would probably take another 5-6 years, at which point it would probably be as messed up as the code we've already got.
Admin
No, no, The First Power was a movie with Lou Diamond Phillips. But more to the point, why do you write it that way instead of just:
int firstPower = 11;
Or is that just part of the whole "paid by the line" theme of this post?
My favorite so far is the Ruby one-liner someone posted.
Admin
certainly, with all the high IQ's here, an AI language is needed: prolog
GetMessage(0,0). GetMessage(4,_) := fail. GetMessage(X,X) := X>0, GetMessage(X-1,X-1). GetMessage(X,0).
hope i remember right... last time i used prolog was 16 years ago.
Admin
I don't know. We don't really know the context in which this code was written. I don't think this guy is an idiot. I think he has a sense of humor. I haven't read through all of the comments, so maybe I'm not the first to say this but, I think this is just a stub function. At some point he would have fleshed it out to do something useful. At this point in the project it was probalby accpetable for it to just return its parameter. He just decided to come with a creative way of doing it. Anyway, just my 2 cents.
Admin
You mean:
Admin
private int GetMessage(int s) {return (s & ~3)?0:s;}
Saved some chars on the paycheck.
Admin
private int GetMessage( int selected ){ return selected 3< ? null : selected; }
Admin
Am I late for this party? Never mind, read it and weep:
Admin
You people need to learn proper exception handling.
#include <excpt.h>
int GetMessage( int selected ) { __try { if ((selected & 0xFFFFFFFC) != 0) { int q = 42; q /= (q-q); } return selected; } __except ( EXCEPTION_EXECUTE_HANDLER ) { return 0; } }
That is the safest and most efficient way of doing this.