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.

Apr 2010

A Vestigial Query

by in CodeSOD on

When people think about "vestigial" organs, they think that we're toting around these useless lumps of flesh that just take up space in our bodies. Nothing could be further from the truth, of course- vestigial organs are organs whose original function has become redundant or superfluous, but generally have adopted some sort of secondary function. Our appendix does nothing to help us digest (its original purpose), but it does some tasks for our immune system and occasionally explodes and kills us.

So it is with our software. As any project evolves, sometimes code becomes vestigial, serving only some minor function and occasionally exploding. Randy discovered this block of code when he was tracing through a production issue.


Something Weird

by in CodeSOD on

Consider the following requirement. A Period can have anywhere from zero to forty eight Activities (though most will have only three) and each Activity within a Period should have a unique, non-sequential, two-digit identifier (e.g. 02, 41, 99).

Now consider how you might implement the code that generates a new identifier. Go ahead, think about it. I'll wait.


sp_getNothing

by in CodeSOD on

"I was working on cleaning up some old code from a system I inherited," writes Anthony Mattas, "the system is an old ASP.NET application that has been around for a while, but won’t be replaced because it generates the staff meeting template for our CEOs weekly leadership meeting."

"The app itself had a lot of redundancy since my company didn’t have a good version control solution or strategy at that time. There was duplicate stored procedures, tables, and pages in the application (this was the old developers' method of version control). Along the way I found this stored procedure:"


Don't Pass The Password

by in CodeSOD on

It’s a generally accepted practice that passwords should be masked when being displayed to users. In addition to preventing over-the-shoulder password snooping, displaying “*****” instead of the actual password gives users a sense of security that somehow, through the magical wizardly of computers, their password is secure. Whether that’s worth the usability hassle, however, is certainly up for debate.

Adam’s colleagues take this practice very seriously, and have gone so far as to mask it from themselves. Their application is a fairly standard three-tier system – database, middle-tier objects, and user interface – and the password masking occurs smack-dab in the middle.


The Doubleton Pattern

by in CodeSOD on

I'm sure many of you are familiar with the Singleton pattern. Simply put, it's a class that can only have a single instance, and the accessor for the instance is global.

Kristopher is certainly no stranger to the pattern, though he was thrown for a bit of a loop when he noticed this often-repeated pattern called Singleton in his new employer's codebase.


Check Digit Check

by in CodeSOD on

Seen any absurdly bad code lately? Send it on in!


Anne K has worked in the direct mail industry for almost twenty years, and has seen a bazillion ways to put names and addresses onto paper. One thing that's common across all mailing houses is that, in order to get discounts from the postal service, a postal barcode must be printed on every piece of mail. The barcode is made up of the ZIP code, the ZIP+4 code, and a couple more digits indicating (often) the house number. There's also a check digit on the bar code, which is calculated by adding up all the digits of the barcode, modding the result by 10, and then subtracting from 10. So, if all the digits of the barcode add up to 39, (10 - (9 mod 10)) leaves a result of 1.

It couldn't be simpler, right? It takes a teeny little loop and a mod. At least, that's what Anne thought. When she was doing developer training at a certain mailhouse, she just happened to be looking into the program that printed a simple name and address block on the piece. Curious as to why it was thousands of lines of code, she dove in and saw this.


Java Destruction

by in CodeSOD on

“It can be difficult to develop complicated, J2EE software without a fundamental understanding of how Java works,” Vladimir writes, “difficult… but not impossible.”

“Following is a super-helpful class found in one of our heavily-used, enterprise beans. I don’t think it works the way the author intended it to.”


Fred Blogs' MegaQuit()

by in CodeSOD on

A few years ago, Gregory was about to start development on a quoting system, when the client pulled-out all of a sudden. “We appreciate all the work you put in so far,” they told him, “but your overall estimate was just too much; we were able to find someone else who could develop this for less than a third of your cost.”

Gregory was surprised, especially since he had bid the system fairly low. He would have considered renegotiating, but the “someone else” they found was Fred Blogs, who happened to be the VP’s son-in-law. And one thing that Gregory had learned over the years is that it’s pretty hard to compete with nepotism.