Orion pointed me to a site on Friday, called the The daily WTF?, and it’s great. It contains totally terrible code snippets that are supposedly pulled from actual production code. This example looks unbelievable, but it’s actually similar to code I have seen before.
When I first started in the eCommerce department back in 1997, the site was re-done by a company called Proxima (they later changed their name to the much more internety Proxicom). As I was browsing around the date code, I noticed a function called getMonthOptions(), which had a similar implementation:
function getMonthOptions(fieldName,currentMonth) {
monthOptions = "";
if ( currentMonth == 1) {
monthOptions += "<option name=\"" +
fieldName +
"\" value=\"1\"" +
" selected="true">January</option>";
} else {
monthOptions += "<option name=\"" +
fieldName + "\" value=\"1\"" +
">January</option>";
}
if ( currentMonth == 2 ){
monthOptions += "<option name=\""
+ fieldName + "\" value=\"2\"" +
" selected="true">February</option>";
} else {
monthOptions += "<option name=\"" +
fieldName + "\" value=\"2\"" +
">February</option>";
}
….and on and on.
So, I replaced that code with something like:
function getMonthOptions(currentMonth) {
var months = ",January,February,March," +
"April,May,June,July,August," +
"September,October,November,December".split(",");
monthOptions = "";
for(var i=1; i < months.length; i++) {
monthOptions += "<option value=\"" +
months[i] + “\” “;
if ( i == parseInt(currentMonth)) {
monthOptions += ” selected=\”true\”";
}
monthOptions += “>” + months[i] + “</option>”;
}
return monthOptions;
}
…and that was that.
Still, it struck me as odd that seasoned consultants would write such verbose code. However, After reading the daily WTF it appears that those consultants were actually pretty good.
The code is good for a laugh or two, but to find what’s really interesting about the site, you have to pull back the covers a little bit. First off, there are a lot of people who totally miss the WTF moment and instead post something like “he names the variables poorly”. That leads me to believe that they probably implemented something like the WTF in the past. Also interesting is the total commitment the administrator has to Microsoft. A while back, the site had a few performance problems, but he couldn’t afford to upgrade the site to Windows Server 2003 with SQL server. The upgrade would have cost him $2500, but he refused to entertain running on open source software. “Dirty hippie software” he derisively called it. What’s amusing is that he’s running some vanilla discussion software and that going open source would have saved him all that grief –someone eventually donated Windows Server Small Business Edition to him, but only after a couple of weeks of protracted whining. Furthermore, his coding skills aren’t that great; At one point, he had to ask how to serialize a HashTable (it’s C#, note the funny capitalization) to a byte array –not really challenging stuff.
Still, it’s a good read and allows me to re-live those days when I would marvel at the ways of the consultants. I might not like every line of code at work but none if it is even remotely as bad as a daily WTF.