Code WTF? - 2011 edition
So today I was helping another developer who was running into some issues in production code - there is a file import that works fine up to a point, but for some reason, a particular file causes it grief.
As of this posting, we still haven’t found the problem - but during lunch, I had to post what we DID find!
As we were debugging through the parsing library, I ran into a bit of code written by Random Developer. At first caught it me off guard, then left me quite literally, Laughing Out Loud.
It starts out simple enough - even has a comment:
Not bad - I think that for this particular process, the Generic is probably overkill - but we will go with it:
Next, lets parse this thing up:
First, if you are using Generics, you have a .NET framework that supports TryParse on all the types **. Yes, a TryParse is a bit more convoluted and requires the use of a generally misunderstood Out parameter - Oh, and a HUGE performance increase if you are parsing a value that fails.
But I’ll over-look that for now, because it is just a sign of what is to come…
So, assuming that the above parse didn’t actually return a properly parsed value, Random Developer decided to add an extra bit of code:
Uh, OK - seems a bit redundant to me - but we haven’t gotten to the best (and LOL) part yet:
Our friend, Random Developer decided to check this value that didn’t parse into a Boolean against a list of 'Yeses' - Huh? List of Yeses?
Yep, Bob’s your Uncle, You Betcha, Damn Skippy - if it is one of these values, it must mean it is TRUE!
At least they used ToLower().
So, Random Developer - you officially made my year on my random code WTF?!
** It also supports String.IsNullOrEmpty - but that’s just a personal preference - instead of individually checking for Null and no Length…