- 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
Perfect example of optimization by loop unrolling! ..only without a loop in the first place
Admin
However silly that function looks, it does map values outside of (1,2,3) to 0.
Admin
Let me guess, the Real WTF (tm) is that he should have converted them to strings and hashed them first, right?
Admin
I'm more confused by it being GetMessage and returning an int.. shouldn't it return a string of some kind? then just return messages[selected].
I'm too jaded to go "WTF?" at this though.
vc: bathe... fine i'll go take a bath =/
Admin
private int GetMessage( int selected ) { int index = 0;
}
Just a little add-on to make it more interesting :-)
Admin
Pfft.
I can write that function using at least 60 lines!
Admin
private int GetMessage( int selected ) { return ((selected < 0 || selected > 3) ? 0 : selected); }
1 liner. Not sure if it gets much better than this.
Admin
I think this is more eloquently styled:
enum {
ZERO = 0, ONE, TWO, THREE
};
volatile const int table[4] = { ZERO, ONE, TWO, THREE };
private int GetMessage( int selected ) { int index = 0;
// Get the Message switch( selected ) {
}
return index;
}
Admin
private int GetMessage( int selected ) { return (selected > 3)? 0 : selected); }
Admin
private int GetMessage( int selected )
{ while((selected<=3)&&(selected>=1)) { return selected; }
}
Why not?
Admin
waits to be shot
Admin
This method is not very extensible. What happens when you want to handle a new message after your application is deployed? Obviously, the best solution is to create a database table whose key is the "selected" value with a record for each "index" value. Then your GetMessage method can just connect to the database, do a SELECT SELECTED_VAL, INDEX_VAL FROM TBL_FOO, and loop through the result set until you find a record with the correct key. </irony>
Admin
I think you meant: switch( selected ) {
case 0: index = table[ZERO]; break;
case 1: index = table[ONE]; break;
case 2: index = table[TWO]; break;
case 3: index = table[THREE]; break;
}
Admin
Just WTF?!
Admin
This is a little clearer:
enum { ZERO = 0, ONE, TWO, THREE };
volatile const int table[4] = { ZERO, ONE, TWO, THREE };
private int GetMessage( int selected ) { int index = 0;
// Get the Message switch( selected ) {
case 0: index = table[selected]; break;
case 1: index = table[selected]; break;
case 2: index = table[selected]; break;
case 3: index = table[selected]; break;
}
return index;
}
Admin
#define BIT_0 0x01; #define BIT_1 0x02; #define BIT_2 0x04; #define BIT_MASK 0x07;
enum { ZERO = 0, ONE, TWO, THREE };
volatile const int table[4] = { ZERO, ONE, TWO, THREE };
private int GetMessage( int selected ) { int temp_selected;
}
Admin
I don't think that goes well for negative values In C maybe something like
int getMessage(int i) return (!(i>>2))*i;
seems to compile in VC++
Admin
Admin
private int GetMessage( int selected ) { return ( selected <= 0 || selected > 3 ? 0 : selected ); }
Admin
private int GetMessage( int selected ) { return (selected == 0) ? 0 : ((selected == 1) ? 1 : ((selected == 2) ? 2 : ((selected == 3) ? 3 : (0)));
}
Admin
What about some recursion, just for fun?
Admin
int GetMessage(int selected) { if (selected > FileNotFound + 1) while (--selected); else if (selected < 0) while(++selected); else switch(selected) { case 2: selected = FileNotFound; break; case 1: selected = False; break; case 0: selected = True; break; }
}
I am still thinking how to get Duff into this...
captcha: pinball
Admin
you are right, maybe add...
if (selected < 0) { value = 0; } else { .... the rest of the code ... }
[shame the size of an int is not defined... and not HIGH_BIT would be fun]
Admin
// private int GetMessage( int selected ) // { // int index = 0; // // // Get the Message // switch( selected ) // { // // case 0: // index = 0; // break; // // case 1: // index = 1; // break; // // case 2: // index = 2; // break; // // case 3: // index = 3; // break; // // } // // return index; // }
yes, just get rid of it.
Admin
return (unsigned)selected > 3 ? 0 : selected;
Admin
Here's another variant, for flavor.
private int GetMessage( int selected ) { static const int ONE = 1; int index = 0;
}
Admin
This is fine with gcc; your milage may vary with other compilers definitions of true:
int getmessage (int value) { return -!(x&~0x3)&x; }
Admin
interface Case { int execute(); }
class Case1Impl implements Case { int execute() { return 1; } }
class Case2Impl implements Case { int execute() { return 2; } }
class Case3Impl implements Case { int execute() { return 3; } }
class DefaultCase implements Case { int execute() {return 0;} }
class CaseFactory { Case getCase(int i) { Case result = new DefaultCase(); try { result = (Case)Class.forName("Case"+i+"Impl").newInstance(); } catch (Throwable e) { } return e; } }
public GetMessage(int selected) { Case case = new CaseFactory().getCase(selected); return case.execute(); }
Admin
static int getMessage(int selected) { try { int[] messages = {0,1,2,3}; return messages[selected]; } catch (ArrayIndexOutOfBoundsException e) { return 0; } }
Admin
My eyes...they burn.
Pass in a sufficiently large value for message and what the call stack explode!
Admin
return Math.abs(index-2) <= 1 ? index : 0
Admin
apologies for relying to myself, but
int high_bit = ((unsigned int)INT_MIN & (unsigned int)INT_MAX);
Admin
OK, more bit-twiddling an added evilness:
int getmessage (int c) { return (!(c&~3))["(c&3)"]&c&3; }
Admin
sorry.... and not that should have been.
Admin
Databases are the ultimate sollution to everything!
private int GetMessage( int selected ) { MYSQL* MySQL = mysql_init(NULL); int index; char Buffer[16000];//16K is always enough. mysql_real_connect(MySQL, "IP", "LOGIN", "PASS", "DB", 0, NULL, 0); mysql_query(MySQL, "DELETE FROM IndexTable"); mysql_query(MySQL, "INSERT INTO IndexTable(ID) VALUES (0)"); mysql_query(MySQL, "INSERT INTO IndexTable(ID) VALUES (1)"); mysql_query(MySQL, "INSERT INTO IndexTable(ID) VALUES (2)"); mysql_query(MySQL, "INSERT INTO IndexTable(ID) VALUES (3)"); sprintf(Buffer, "SELECT * FROM IndexTable WHERE ID = %i", selected); mysql_query(MySQL, Buffer); MYSQL_RES* Res = mysql_store_result(MySQL); if (!Res) return 0; MYSQL_ROW Row = mysql_fetch_row(Res); index = atoi(Row[0]); mysql_free_result(Res); mysql_close(MySQL); return index; }
Admin
Silly people, it needs to be an object!
Static int getMessage(int selected) { SomeSillyObject s = new SomeSillyObject(); return s.getMessage(selected); }
That way you can implement encryption and message translation in the SillyObject and not mess with your carefully crafted business code. And what about selections that aren't integers? Well, you're covered there too!
Of course you could write it without making an instance of the class, but hey, I'm getting paid by the line here.
Admin
private int GetMessage(int selected) { return (selected < 0) ? GetMessage(0) : (selected > 3) ? GetMessage (0) : (selected == 0) ? 0 : GetMessage(selected - 1) + 1; }
'nuff said. :-)
Admin
My C is a bit rusty, but I guess this works:
Added wtfs: Might or might not work depending on the system it's ran on, strange mix of boolean values and integers.
Admin
captcha: onomatopoeia Now that's just mean; makin' me type all that
Admin
That is the most evil thing I have ever seen... it does compile and work though ;-)
Admin
A mere four values, or was this cut down for clarity?
In my first SW job back in the previous millennium, I inherited code like this:
switch ( someInt ) { case 1: k = 1; someFunction(k, otherArgs); break; case 2: k = 2; someFunction(k, otherArgs); break; /* ...etc... ad nauseam and then some */ }
This beastly contraption had FORTY (40) cases. Literally. All they did was an identical function call, after setting k to the case constant.
I was a barelytwenty trainee, still at university, and not even studying CS/programming (I'm a mathematician). The guy who had conjured that code was twice my age and supposedly a senior. He even had the nerve to resist my cutting the switch-case away, lest my edits break the code.
He also had a habit of always passing function arguments from variables with the same name as what the parameter was named in the definition of the function. So if he had, say,
int fooFunc( int bar, char quux ) { .... }
he would always call it like this:
He claimed that otherwise it becomes too difficult to follow what goes where...
Admin
That's certainly the shortest, but I've found that people maintaining my code get confused by all but the simplest ternary statements. So, I'd do:
(Hey! this forum software puts extra newlines in the code, at least when I post using Firefox on Windows: maybe the /r/n is converting to two breaks?)
Just for fun, though, the inverse of yours is:
Admin
public class WTF { private static HashMap<Integer, Integer> theMap;
public WTF() { if (theMap == null) { theMap = new HashMap()<Integer,Integer>; // no need to set size, Java will do this automagically for (int index = Integer.minValue(); index <= Integer.maxValue; index++) { switch(index) { case 0: theMap.put(new Integer(index), new Integer(0)); break; case 1: theMap.put(new Integer(index), new Integer(1)); break; case 2: theMap.put(new Integer(index), new Integer(2)); break; case 3: theMap.put(new Integer(index), new Integer(3)); break; default: theMap.put(new Integer(index), new Integer(0); }//end switch }//end for }//end if }//end constructor
public int GetMessage(int selected) { Integer requested = new Integer(selected); Integer theMessage; theMessage = theMap.get(requested); return theMessage.intValue(); }
}
Admin
Seems like a totally reasonable example of 'belt-and-braces' to me.
Just because the expected values for 'selected' happen to map directly to the return values currently, it doesn't mean these won't change in the future.
Top marks to "Nicholas" for capturing this subtle business requirement [even it looks a bit silly!]
Not very agile though :)
Admin
Here is another fun varient -- an abominable use of recursion.
private int GetMessage( int selected ) { return GetMessage_R( selected, 0 ); }
private int GetMessage_R( int selected, int count ) { if( count >= selected ) return count; return GetMessage_R( selected, count+1 ); }
Admin
Admin
bwahahaha... I'd agree man, who would tarnish their image with such a gimmick?
Admin
Sorry, that should be MAX_VALUE and MIN_VALUE
// couldn't be bothered to compile.
Admin
I have never seen that before, but that is a beautiful piece of mathematics. I have never thought about it before, but it makes sense.
What's the bigger of two numbers? Find the midpoint between the two numbers, and then add half the distance.
Subtracting half the distance yields the minimum.
Kudos to you for sharing that with me!
Admin
If LOC is the metric...
int getMessage(int code) { if (code==-2147483648) return 0; else if (code==-2147483647) return 0; else if (code==-2147483646) return 0;
}