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.

Sep 2011

The Global Workaround

by in CodeSOD on

"In a strictly object oriented environment like C#/.NET," writes J.D., "there's really no such thing as 'global variables'. Sure, you can create a public class called 'Global' with a bunch of static fields, but the folks looking to (ab)use global variables generally have a hard time making that conceptual leap."

"I work with those people each and every day — most of them senior to me — and while they haven't quite made that conceptual leap, they have discovered a workaround."


Python Charmer

by in CodeSOD on

"I don't have a whole lot of experience in Python," writes Jakob, "in fact, when I was hired, the only thing I knew about the language was that whitespace was important."

"Fortunately — actually, unfortunately — it doesn't take a whole lot of experience in Python to recognize that my company's codebase is... well... sub-optimal. Submitted for your approval is a method used to generate a password, found in our network security library."


The Importance Scale and Dianne Wegg

by in CodeSOD on

The finance team at Matthew Riches's company has a unique way of determining which requests go to the top of their list and which ones the bottom. Instead of sorting by "date requested", their solution is to use a weighting function that assigns an importance value based on who made the request. It's nothing fancy: just a bunch of items in a database.

This system seemed to work for many years until the finance department was getting increasingly annoyed that a new starter in the IT department was able to get higher up the chain compared to his peers. This resulted in all finance requests from IT channeling through this single person, as it was likely to get reviewed sooner. As a new developer Matthew was asked to look into this weighting function as, no matter what the finance department did, changing the values in the database seems to have no effect.


Clone Collector

by in CodeSOD on

Like the Rules of Optimization, there are two rules to follow for the touching .NET's Garbage Collector: (1) Don't, and (2) Be careful (for experts only). Most memory-related problems that developers experience in .NET do not require intervention of GC, but more an injection of sane code.

Joe's coworker — and self-proclaimed expert — was getting some pesky OutOfMemoryException errors while cloning objects. Knowing that it had to be a problem with garbage collection, he concocted this innovative solution.


The Human-Readable Encryption Key

by in CodeSOD on

"One of the problems with encryption keys," notes Steve, "is that they're generally just a set of bytes, and humans can't read bytes. While one could certainly represent those bytes with a hexadecimal string, or even a little base64, it still looks like machine-language gobbledygook."

"Fortunately, VMware's Customization Specification Manager was able to solve this problem through the power of XML. Behold the encryptionKey and its simple, human-readable XML representation."


Verify[most]DOB()

by in CodeSOD on

"Back in 2001, I worked for a company that brought on a couple dozen highly-paid consultants to set up a series of websites," Mark Steyn writes, "I was one of a small number of permanent employees working alongside the consultants, and it quickly became clear that consultancy supplied us with a number of extremely talented developers who knew exactly what needed to done and how best to accomplish it. Unfortunately that number was 1."

"The remainder were... somewhat less capable. I'll cite as evidence this JavaScript date validation function I stumbled across when a bug report noted that a tester had experienced problems trying to sign up for the site. Their date of birth was consistently rejected. I forget the exact date, save that they were born on the 28th.