Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Jan 2008

Coding Like the Tour de France

by in CodeSOD on

Last year, while watching coverage of the Tour de France, Stephan M noticed something a little strange. If he tilted his head by 90° and squinted just a bit, the profile maps showing the peaks and valleys of the mountain stage looked rather familiar. They were just like the monstrous routines written by his predecessor - the very same coder behind the Quadrasort.

"Following is a stripped down version of an almost 700 LoC method in Java," wrote Stephan," I removed anything except the control flow statements, so that you can grasp the full beauty of this masterpiece.


Elegant Syntax Error

by in CodeSOD on

At some point, you've probably thought to yourself "I'd love to write some code, but the keywords in my language of choice are just so pedestrian." Your vocabulary is more embiggoned than that of your colleagues. While they're content with DROPping a database object, you'd prefer to efface it. A contractor that Charles I.'s worked with was one of these elite.

His first order of business? Get rid of those (ugh) "while" loops that his mouth-breathing coworkers used. With the belief that elegant code should embody every aspect of the word "elegant," he wrote all of his "while" loops as "whilst" loops.


Right Under your Nose

by in CodeSOD on

There are a lot of reasons to reinvent software. Maybe you don't trust the person who wrote it in the first place. Maybe you wrote it back when you didn't know what you were doing, but this time you'll get it right. Or maybe you didn't know that the solution was built already, right under your nose the whole time.

H. Y.'s colleague, as far as he could tell, fell into that third camp. He had a problem — he needed to read a value from web.config (an XML file that holds common settings for .NET applications, like authentication, authorization, connection strings, etc.) — and skipped past the research phase directly to solution engineering.


OnClick Does What?!

by in CodeSOD on

When Doug D. was asked to investigate a data truncation issue, he figured it'd be pretty easy. He clicked through the application to test it out, and discovered that validation was only handled client side. After turning JavaScript off, he could submit text boxes with more text than would fit in the database column.

There was just one problem; he didn't see the client-side validation code next to the other functions defined on the page. He scrolled down to the submit button to see what it did in its OnClick.


OurBoolean

by in CodeSOD on

"While digging through some database code in our system," Paul L wrote in, "I noticed an interesting pattern emerge. Whenever a boolean value within a SQL result was checked, it would look something like this..."

if (result != null && result.toUpperCase.equals(OurBoolean.TRUE))
{ 
    ...
}

Far Too Fancy

by in CodeSOD on

Although the .NET Framework ships with a comprehensive XML library, Sam B's coworkers aren't big fans of it. It's far too fancy, they claim. Instead, they prefer to use StringBuilders, concatenation, and IndexOf(">")-style parsing.

"What on Earth do we need XML-Navigation and Nodes for," they'll often rhetorically say, "come on, it's all just string manipulation!" As it turns out, "XML-Navigation" and "Nodes" certainly do come in handy, especially when one needs to, say, navigate nodes in XML, as they'll often have do.


Easy Authentication

by in CodeSOD on

Forget your password? Don't worry, you can still use the original invitation password that was sent to you when your account was created!

Curious to try out another's account? Don't worry, you can use the original invitation password to log in as anyone!


These Go To Fourteen

by in CodeSOD on

"Actually," the lead on Chris G's team said defensively, "this is a quite elegant solution for displaying data in a JSP. This way, we don't have to use the real object and can save memory."

And unlike the previous version checked in source control that only went up to ten, this ListBean goes to fourteen...


The Mostest Wrong Datatype

by in CodeSOD on

While working a contract at a small educational institution this past summer, Dave R. was asked to "clean up" some of the PHP code behind the institution's main web site. In particular, the IT manager asked if course listings couldn't be sorted so that courses appeared ordered by their starting dates.

The web site had been written by five or six different contractors over the past few years, and the most recent and capable of these had declared that it “was not feasible” to sort the course listings by date.