views:

554

answers:

5

Teller (of Penn and Teller fame) used to double- and triple-stack magic tricks for the pure sake of making it appear as though he was doing something totally mundane - like placing a ball under a cup. He'd do it by a string of crazy palmings, misdirections, and slight-of-hand... he was, in a way, obfuscating his obfuscations.

On occasion, I've had the need to scratch the magically mundane itch: write something out the long way that I would usually get for free. In the following case (already part of a fibonacci thread,) I had a little fun with implementing a call stack and program counter for myself:

int s[999999];
int fib(int i)
{
_1: int t=0; s[t++] = i; s[t++] = 0; s[t] = 0;
_10: if (s[t-2] < 2) { s[t-2] = 1; t-=3; }
_20: if (t < 0) return s[0];
_30: if (s[t] == 1) goto _60;
_40: if (s[t] == 2) goto _70;
_50: s[t++]++; s[t++]=s[t-3]-1; s[t++]=0; s[t]=0; goto _10;
_60: s[t-1]=s[t+1]; s[t++]++; s[t++]=s[t-3]-2; s[t++]=0; s[t]=0; goto _10;
_70: s[t-2]=s[t-1]+s[t+1]; t-=3; goto _20;
}

So the question is really an open call for submissions of your most gratuitous treatments of the simplest concepts in programming. Do your worst.

+9  A: 

The Daily WTF... Oh, sorry, you meant INTENTIONAL? =)

+3  A: 

We keep threatening to put together a "plusup" package here to allow for operations like:

assert plusUp(5, 7) == 12;

We've filled it out with methods like plussUpByOne(x) and subDown(a, b).

Every so often we end up with "Free code" in our company newsletter from the "plusUp" package.

Edit: I just remembered, I had an assignment in college in a Pascal based class. The teacher kept going off about how horrible Basic was and how awesome Pascal was.

The assignment wasn't bad, a few subroutines and some loops. I ended up coding the entire Pascal project using line numbers and gotos, not even a single function call or loop. Then I coded the entire thing in Basic (QBasic where you can use functions and calls, etc. Not a single goto of course). I turned them both in together.

This is probably the only programming project I didn't get an "A" on, actually got an "F" (even though he hadn't given us specific style requirements and the programs both worked flawlessly).

He was pissed off. Bet he never even realized how much work it took to write code THAT spaghetti!

Bill K
A: 

Things like

if (strcmp(s1, s2) != 0) {
}

are perfectly useless from C compiler point of view, but I do it anyway. Does this qualify?

Arkadiy
Code isn't written for the compiler, it's written for the next coder to pick it up. That said, you're right--this should be almost harder for a C programmer to understand than the obvious "C" way.
Bill K
+1  A: 

Does it count that I write overly-optimised functions for things that I know I'm probably never going to use again?

For example, I wanted to output the ordinal suffix (the 'nd' in '2nd' etc) for a number in a site once, and wrote a function for it (instead of just hacking it out of the date function). I don't think I've ever used it since, but I just enjoyed working out the logic to get it into an efficient one-liner...

JoeBloggs
+2  A: 

Numbers in C++ aren't objects. Everything should be objects. I implemented a calculator without any numbers in the code.

class Number
{
public:
    Number() : is_negative(false){}
    virtual ~Number() {}
    virtual char* to_s();

    virtual Number* operator+(Number& other) = NULL;
    virtual Number* operator-(Number& other);
    virtual Number* operator*(Number& other) = NULL;
    virtual Number* operator/(Number& other);

    virtual Number* addOne();
    virtual Number* subtractOne();
    virtual Number* inverse();

    virtual bool zero() {return false;};
    virtual bool negative() {return is_negative; }
    virtual bool setNegative(bool isNegative) { return is_negative = isNegative; }
    /*...*/
protected:
    bool is_negative;
};
class Zero : public Number;
class One : public Number;
class Two : public Number;
/*....*/
Number* Number::operator/(Number& other)
{
    One one;
    return *this * *(one/other);
}
Number* Number::addOne()
{
    One one;
    return one+*this;
}
/* ... */
Number* Zero::addOne()
{
    return new One;
}
/*...*/    
/* Class One */
Number* One::operator*(Number& other)
{
    return other.Clone(); 
}
Number* One::addOne()
{
    return new Two;
}
/*...*/
Number* Three::operator*(Number& other)
{
    Two two;
    return *other.Clone() + *(two * other);
} 
/*...*/
/* Class Nine */
Number* Nine::operator+(Number& other)
{
    One one;
    Eight eight;
    return eight + *(one + other);
}
Number* Nine::operator*(Number& other)
{
    Three three;
    return three * *(three *other);
}
Number* Nine::addOne()
{
    return new MultidigitNumber(new One, new Zero);
}

and on and on... It actually worked, for some limited definition of 'work'

AShelly