• (cs)

    Perfect example of optimization by loop unrolling! ..only without a loop in the first place

  • The Swami of Salami (unregistered)

    However silly that function looks, it does map values outside of (1,2,3) to 0.

  • Welbog (unregistered)

    Let me guess, the Real WTF (tm) is that he should have converted them to strings and hashed them first, right?

  • Nanashi (unregistered)
      private int GetMessage( int selected )
      {
          return selected;
      }
    

    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 =/

  • (cs)

    private int GetMessage( int selected ) { int index = 0;

      for (int counter = 0; counter <= selected; counter++) {
         // Got the Message?
         switch( counter )
         {
    
          case 0:
              index = 0;
              break;
    
          case 1:
              index = 1;
              break;
    
          case 2:
              index = 2;
              break;
    
          case 3:
              index = 3;
              break;
    
        }
      }
      
    
      return index;
    

    }

    Just a little add-on to make it more interesting :-)

  • (cs)

    Pfft.

    I can write that function using at least 60 lines!

  • Bryan (unregistered)

    private int GetMessage( int selected ) { return ((selected < 0 || selected > 3) ? 0 : selected); }

    1 liner. Not sure if it gets much better than this.

  • jensenjoe (unregistered)

    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 ) {

      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;

    }

  • Anontoo (unregistered) in reply to Bryan

    private int GetMessage( int selected ) { return (selected > 3)? 0 : selected); }

  • Meh (unregistered)

    private int GetMessage( int selected )

    { while((selected<=3)&&(selected>=1)) { return selected; }

      return 0;
    

    }

    Why not?

  • hpeg (unregistered) in reply to Bryan
    Bryan:
    private int GetMessage( int selected ) { return ((selected < 0 || selected > 3) ? 0 : selected); }

    1 liner. Not sure if it gets much better than this.

    145,146c14,1
    < int result = GetMessage( moose );
    ---
    > int result = Math.min( Math.max(moose, 0), 3 );
    

    waits to be shot

  • Emphyrio (unregistered)

    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>

  • code fixer (unregistered) in reply to jensenjoe
    jensenjoe:
    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 ) {

      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;

    }

    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;

    }

  • Haydar Ciftci (unregistered)

    Just WTF?!

  • Peter Antoine (unregistered) in reply to jensenjoe

    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;

    }

  • Peter Antoine (unregistered) in reply to code fixer

    #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;

    temp_selected = (selected & BIT_MASK);
    
    if ((temp_selected & BIT_0) && !(temp_selected & BIT_1) && !(temp_selected & BIT_2))
    {
    	value = ONE;
    }
    else if (!(temp_selected & BIT_0) && (temp_selected & BIT_1) && !(temp_selected & BIT_2))
    {
    	value = TWO;
    }
    else if (!(temp_selected & BIT_0) && !(temp_selected & BIT_1) && (temp_selected & BIT_2))
    {
    	value = THREE;
    }
    else
    {
    	value = ZERO;
    }
    
    
    int index = 0;
    
    // Get the Message
    switch( value )
    {
    
    	case 0:
    		index = table[value];
    		break;
    
    	case 1:
    		index = table[value];
    		break;
    
    	case 2:
    		index = table[value];
    		break;
    
    	case 3:
    		index = table[value];
    		break;
    
    }
    
    return index;
    

    }

  • D2orus (unregistered) in reply to hpeg

    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++

  • (cs) in reply to hpeg
    hpeg:
    145,146c14,1
    < int result = GetMessage( moose );
    ---
    > int result = Math.min( Math.max(moose, 0), 3 );
    
    ((moose + abs(moose)) / 2 + 3 - abs((moose + abs(moose)) / 2 - 3)) / 2
    
    Hint : max(a,b) = (a + b + abs(a - b)) /2 and min(a,b) = (a + b - abs(a - b)) / 2
  • Leo (unregistered)

    private int GetMessage( int selected ) { return ( selected <= 0 || selected > 3 ? 0 : selected ); }

  • mav (unregistered)

    private int GetMessage( int selected ) { return (selected == 0) ? 0 : ((selected == 1) ? 1 : ((selected == 2) ? 2 : ((selected == 3) ? 3 : (0)));

    }

  • Yariv (unregistered)

    What about some recursion, just for fun?

    int BIT_MASK = 0x3;
    
    int getMessage(int message) {
      if (BIT_MASK & message != message)
        return 0;
      if (message = 0)
        return 0;
      return getMessage(message-1)+1;
    }
    
  • Hob (unregistered)

    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; }

    	return selected;
    

    }

    I am still thinking how to get Duff into this...

    captcha: pinball

  • Peter Antoine (unregistered) in reply to D2orus
    D2orus:
    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++

    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]

  • Voodoo (unregistered)

    // 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.

  • zhe (unregistered)

    return (unsigned)selected > 3 ? 0 : selected;

  • Optimizer (unregistered)

    Here's another variant, for flavor.

    private int GetMessage( int selected ) { static const int ONE = 1; int index = 0;

    // Get the Message
    switch( selected )
    {
        case 0: index += ONE;
        case 1: index += ONE;
        case 2: index += ONE;
        case 3: index += ONE;
    }
    
    return index;
    

    }

  • Paul J (unregistered)

    This is fine with gcc; your milage may vary with other compilers definitions of true:

    int getmessage (int value) { return -!(x&~0x3)&x; }

  • Buben Razuma (unregistered)

    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(); }

  • (cs)

    static int getMessage(int selected) { try { int[] messages = {0,1,2,3}; return messages[selected]; } catch (ArrayIndexOutOfBoundsException e) { return 0; } }

  • Anonymous (unregistered) in reply to Yariv

    My eyes...they burn.

    Pass in a sufficiently large value for message and what the call stack explode!

  • (unregistered)

    return Math.abs(index-2) <= 1 ? index : 0

  • Peter Antoine (unregistered) in reply to Peter Antoine
    Peter Antoine:
    D2orus:
    I don't think that goes well for negative values

    [shame the size of an int is not defined... and not HIGH_BIT would be fun]

    apologies for relying to myself, but

    int high_bit = ((unsigned int)INT_MIN & (unsigned int)INT_MAX);

  • Paul J (unregistered)

    OK, more bit-twiddling an added evilness:

    int getmessage (int c) { return (!(c&~3))["(c&3)"]&c&3; }

  • Peter Antoine (unregistered) in reply to Peter Antoine

    sorry.... and not that should have been.

  • (cs)

    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; }

  • Duston (unregistered)

    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.

  • mlq (unregistered)

    private int GetMessage(int selected) { return (selected < 0) ? GetMessage(0) : (selected > 3) ? GetMessage (0) : (selected == 0) ? 0 : GetMessage(selected - 1) + 1; }

    'nuff said. :-)

  • (cs)

    My C is a bit rusty, but I guess this works:

    int getMessage(int selected) { 
            return ( ( selected & 0x03 ) * !( selected & 0x7ffffffc ) );
    }
    

    Added wtfs: Might or might not work depending on the system it's ran on, strange mix of boolean values and integers.

  • MGS (unregistered)
    static int getMessage(int selected) { 
        int index;
        if (! selected > 3
         && ! selected == 3
         && ! selected == 2
         && ! selected == 1
         && ! selected < 0
         && selected = 0) { index = 0 }
        else if (! selected > 3
         && ! selected == 3
         && ! selected == 2
         && ! selected == 0
         && ! selected < 0
         && selected = 1) { index = 1 }
        else if (! selected > 3
         && ! selected == 3
         && ! selected == 1
         && ! selected == 0
         && ! selected < 0
         && selected = 2) { index = 2 }
        else if (! selected > 3
         && ! selected == 2
         && ! selected == 1
         && ! selected == 0
         && ! selected < 0
         && selected = 3) { index = 3 }
        else if (! selected == 3
         && ! selected == 2
         && ! selected == 1
         && ! selected == 0
         && ! selected < 0
         && selected > 3) { index = 0 }
    
        return index
    }
    

    captcha: onomatopoeia Now that's just mean; makin' me type all that

  • bzr (unregistered) in reply to Paul J
    Paul J:
    OK, more bit-twiddling an added evilness:

    int getmessage (int c) { return (!(c&~3))["(c&3)"]&c&3; }

    That is the most evil thing I have ever seen... it does compile and work though ;-)

  • TOK (unregistered)

    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:

    int bar = whatever;
    char quux = 'q';
    
    fooFunc( bar, quux );
    

    He claimed that otherwise it becomes too difficult to follow what goes where...

  • RadiantMatrix (unregistered) in reply to Bryan
    Bryan:
    private int GetMessage( int selected ) { return ((selected < 0 || selected > 3) ? 0 : selected); }

    1 liner. Not sure if it gets much better than this.

    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:

    private int GetMessage( int selected ) {
        if ( selected >= 0 && selected <= 3 )
            return selected;
        else
            return 0;
    }
    

    (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:

    return ( (selected >= 0 && selected <= 3) ? selected : 0 );
  • Look at me! I'm on the internets! (unregistered)

    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(); }

    }

  • Stewart (unregistered)

    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 :)

  • Khomar (unregistered)

    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 ); }

  • Joe H. (unregistered)
    public static int getMessage(int s)
    {
    return s<0?0:s>3?0:s;
    }
    
  • barfing (unregistered)
    anon:
    can't be real

    bwahahaha... I'd agree man, who would tarnish their image with such a gimmick?

  • Look at me! I'm on the internets! (unregistered) in reply to Look at me! I'm on the internets!
    Look at me! I'm on the internets!:
    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(); }

    }

    Sorry, that should be MAX_VALUE and MIN_VALUE

    // couldn't be bothered to compile.

  • (cs) in reply to acne
    acne:
    Hint : max(a,b) = (a + b + abs(a - b)) /2 and min(a,b) = (a + b - abs(a - b)) / 2

    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!

  • rab (unregistered)

    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;

    /* snip others 2 billions else if's */
    
    else if (code==0) return 0;
    else if (code==1) return 1;
    else if (code==2) return 2;
    else if (code==3) return 3;
    
    else if (code==4) return 0;
    else if (code==5) return 0; 
    
    /* snipped the others 2 billions .. */
    
    else if (code==2147483645) return 0;
    else if (code==2147483646) return 0;
    else if (code==2147483647) return 0;
    else
        /* Ignore everything in range ]min(int),max(int)[ */
        return 0; 
    

    }

Leave a comment on “Paid by the Line”

Log In or post as a guest

Replying to comment #:

« Return to Article