Yearly WTF – the most terrible code I’ve seen in months

Today I came across the most terrible piece of code I’ve seen in months. Years. One would suspect the original programmer deliberately intended to cause chaos and despair.

It starts off with some Hungarian notation to throw you off-balance; check out the “strBetaald” variable which seems to be a string but is in fact a boolean:

string strValue = "";
string strCheck = "";
string strPaid = "";
string strProcess = "";
bool strBetaald = false;

Then the code proceeds to loop through XML nodes, setting the variable “strValue” to a certain value if the XML node name is X, and setting it to another value if the node name is Y. At the end of the run, strValue contains… what value? Well, doh, that depends on whether we came across X or Y first! The variable probably isn’t that important, since it just gets logged in a financial application database in approximately 50 lines down or so. Hey, how about a small coffee break, anyone?

In the middle of the loop we find the following gold nugget:

if (strCheck != "true".ToString())
    strCheck = strCheck;
    strCheck = strCheck;
    // some other stuff..
bool bet = Convert.ToBoolean(strBetaald);
bool chk = Convert.ToBoolean(strCheck);

So here we’re converting the string “true” to yet another string (just to be on the safe side) and to be really sure about getting the correct value, we assign the variable to itself.

Finally, we’re converting the boolean strBetaald to boolean, to be absolutely completely 100% positively certain that we’re right about this whole true/false thing because somewhere we read that there’s this weird thing called fuzzy logic and it may be lurking inside the compiler somewhere.

It all does make sense, so why am I so worked up about this?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: