Comment On Paid by the Line

Frank is a contractor. His company has been brought in to refactor some back-end web services that were all developed by a single person, "Nicholas," over the course of a couple of years. [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6Next »

Re: Paid by the Line

2007-01-31 09:04 • by Aerendel
Perfect example of optimization by loop unrolling! ..only without a loop in the first place

Re: Paid by the Line

2007-01-31 09:07 • by The Swami of Salami (unregistered)
However silly that function looks, it does map values outside of (1,2,3) to 0.

Re: Paid by the Line

2007-01-31 09:07 • by Welbog (unregistered)
Let me guess, the Real WTF (tm) is that he should have converted them to strings and hashed them first, right?

Re: Paid by the Line

2007-01-31 09:08 • by 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 =/

Re: Paid by the Line

2007-01-31 09:09 • by TheRider
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 :-)

Re: Paid by the Line

2007-01-31 09:10 • by Arancaytar
Pfft.

I can write that function using at least 60 lines!

Re: Paid by the Line

2007-01-31 09:11 • by 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.

Re: Paid by the Line

2007-01-31 09:14 • by 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;

}


Re: Paid by the Line

2007-01-31 09:15 • by Anontoo (unregistered)
114637 in reply to 114633
private int GetMessage( int selected )
{
return (selected > 3)? 0 : selected);
}

Re: Paid by the Line

2007-01-31 09:16 • by Meh (unregistered)

private int GetMessage( int selected )

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

return 0;
}


Why not?

Re: Paid by the Line

2007-01-31 09:17 • by hpeg (unregistered)
114639 in reply to 114633
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*

Re: Paid by the Line

2007-01-31 09:21 • by 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>

Re: Paid by the Line

2007-01-31 09:23 • by code fixer (unregistered)
114641 in reply to 114634
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;

}

Re: Paid by the Line

2007-01-31 09:25 • by Haydar Ciftci (unregistered)
Just WTF?!

Re: Paid by the Line

2007-01-31 09:25 • by Peter Antoine (unregistered)
114643 in reply to 114634
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;

}

Re: Paid by the Line

2007-01-31 09:27 • by Peter Antoine (unregistered)
114644 in reply to 114641
#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;

}

Re: Paid by the Line

2007-01-31 09:27 • by D2orus (unregistered)
114645 in reply to 114639
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++

Re: Paid by the Line

2007-01-31 09:33 • by acne
114646 in reply to 114639
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

Re: Paid by the Line

2007-01-31 09:35 • by Leo (unregistered)
private int GetMessage( int selected )
{
return ( selected <= 0 || selected > 3 ? 0 : selected );
}

Re: Paid by the Line

2007-01-31 09:36 • by mav (unregistered)
private int GetMessage( int selected )
{
return (selected == 0) ? 0 :
((selected == 1) ? 1 :
((selected == 2) ? 2 :
((selected == 3) ? 3 :
(0)));

}

Re: Paid by the Line

2007-01-31 09:37 • by 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;
}

Re: Paid by the Line

2007-01-31 09:38 • by 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

Re: Paid by the Line

2007-01-31 09:38 • by Peter Antoine (unregistered)
114651 in reply to 114645
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]

My rewrite

2007-01-31 09:43 • by 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.

Re: Paid by the Line

2007-01-31 09:43 • by zhe (unregistered)
return (unsigned)selected > 3 ? 0 : selected;

Re: Paid by the Line

2007-01-31 09:43 • by 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;
}

Re: Paid by the Line

2007-01-31 09:43 • by 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;
}

Re: Paid by the Line

2007-01-31 09:44 • by 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();
}

Re: Paid by the Line

2007-01-31 09:44 • by Licky Lindsay
static int getMessage(int selected) {
try {
int[] messages = {0,1,2,3};
return messages[selected];
} catch (ArrayIndexOutOfBoundsException e) {
return 0;
}
}

Re: Paid by the Line

2007-01-31 09:45 • by Anonymous (unregistered)
114658 in reply to 114649
My eyes...they burn.

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

Re: Paid by the Line

2007-01-31 09:51 • by (unregistered)
return Math.abs(index-2) <= 1 ? index : 0

Re: Paid by the Line

2007-01-31 09:55 • by Peter Antoine (unregistered)
114660 in reply to 114651
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);

Re: Paid by the Line

2007-01-31 09:55 • by Paul J (unregistered)
OK, more bit-twiddling an added evilness:

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

Re: Paid by the Line

2007-01-31 09:57 • by Peter Antoine (unregistered)
114662 in reply to 114660
sorry.... and not that should have been.

Re: Paid by the Line

2007-01-31 09:59 • by Daid
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;
}

Re: Paid by the Line

2007-01-31 10:01 • by 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.

Re: Paid by the Line

2007-01-31 10:01 • by 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. :-)

Re: Paid by the Line

2007-01-31 10:04 • by halcyon
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.

Re: Paid by the Line

2007-01-31 10:04 • by 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

Re: Paid by the Line

2007-01-31 10:06 • by bzr (unregistered)
114669 in reply to 114661
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 ;-)

Re: Paid by the Line

2007-01-31 10:07 • by 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...

Re: Paid by the Line

2007-01-31 10:08 • by RadiantMatrix (unregistered)
114671 in reply to 114633
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 );

Re: Paid by the Line

2007-01-31 10:10 • by 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();
}

}

Re: Paid by the Line

2007-01-31 10:10 • by 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 :)

Re: Paid by the Line

2007-01-31 10:10 • by 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 );
}



Re: Paid by the Line

2007-01-31 10:11 • by Joe H. (unregistered)
public static int getMessage(int s)
{
return s<0?0:s>3?0:s;
}

Re: Paid by the Line

2007-01-31 10:12 • by barfing (unregistered)
anon:
can't be real



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

Re: Paid by the Line

2007-01-31 10:12 • by Look at me! I'm on the internets! (unregistered)
114678 in reply to 114673
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.

Re: Paid by the Line

2007-01-31 10:12 • by Benanov
114679 in reply to 114646
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!

Re: Paid by the Line

2007-01-31 10:12 • by 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;
}
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6Next »

Add Comment