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.

Mar 2008

Jed Code

by in CodeSOD on

"About two years ago," Simon writes, "I worked for a small telecommunications company. Turnover was fairly high, leaving not much consistency in the way applications were developed."

"Shortly after starting, I was tasked with fixing a report. For whatever reason, it wouldn't show any data newer than two months. So, I went to the reporting website and loaded up the report page. Eight minutes of watching the hourglass cursor turn, I verified that the report did not, in fact, display any new data. So I dug in to the report code.


Barely Missing the Concept

by in CodeSOD on

Sometimes you see some code and can feel the frustration of the original developer. Sometimes it's because of profanity-laden comments in the code, other times it's because you can tell that they were right on the brink of a major breakthrough, but gave up.

In this case from Oliver K., the developer did his research, found the right tools for the job, and started coding. He realized that the task would be best handled with interfaces and probably toiled for hours trying to figure them out, eventually checking in the following:


N-Replace Zero-Test

by in CodeSOD on

Thomas Nordlander writes: "The Swedish Church of Scientology's scary personality test contains some pretty awesome JavaScript validation. Consider the ingenious way that they make sure they are dealing with numbers."

"I don't think I've ever seen the N-Replace Zero-Test pattern before..."


Not So Simple

by in CodeSOD on

"It should be pretty simp--" David M cut himself off. He learned his lesson. Nothing at his new job was Pretty Simple.

Earlier that day, David decided to pick up a task from back log called the "Search For Location" feature. He figured it'd be a quick thirty minutes of coding: use the existing Location class, write a new stored procedure for the search, add a sproc-wrapper in the data provider layer, and leverage the existing Data-to-Object mapper code. Then he'd just have to write a new web service method -- SearchForLocationByName(string location) -- to call the DataProvider, reconcile with the LocationMapper, and chuck out the returned collection. Easy, right? There was just one problem: David could not find the "Location" table.


Type Safety Considered Harmful

by in CodeSOD on

Submitted anonymously: "I found this in a data access layer for an application testing framework. The object has fields that are obviously meant to contain integers, doubles, and DateTime objects, but they are all strings and this is the only constructor:

class LineItem
{
    // SNIP: 28 public string fields
    public LineItem(Object[] fields)
    {
        int index = 0;

        //limit initial memory assigned for line items due to infrequent use
        lineItems = new List(0);

        foreach (Object field in fields)
        {
            switch (index++)
            {
                case 0: category = field.ToString(); break;
                case 1: paymentMethod = field.ToString(); break;
                case 2: transDate = field.ToString(); break;
                case 3: amountCurrency = field.ToString(); break;
                case 4: exchangeCode = field.ToString(); break;
                case 5: exchangeRate = field.ToString(); break;
                case 6: documentNumber = field.ToString(); break;
                case 7: location = field.ToString(); break;
                case 8: dateFrom = field.ToString(); break;
                case 9: dateTo = field.ToString(); break;
                case 10: deduction_breakfast = field.ToString(); break;
                case 11: deduktion_lunch = field.ToString(); break;
                case 12: deduktion_dinner = field.ToString(); break;
                case 13: mealDeduktion = field.ToString(); break;
                case 14: accountNumber = field.ToString(); break;
                case 15: projectId = field.ToString(); break;
                case 16: description = field.ToString(); break;
                case 17: salesTaxGroup = field.ToString(); break;
                case 18: itemSalesTaxGroup = field.ToString(); break;
                case 19: additionalInformation = field.ToString(); break;
                case 20: miles = field.ToString(); break;
                case 21: rate_per_mile = field.ToString(); break;
                case 22: advanceRequestedDate = field.ToString(); break;
                case 23: department = field.ToString(); break;
                case 24: cost_center = field.ToString(); break;
                case 25: purpose = field.ToString(); break;
                case 26: timeFrom = field.ToString(); break;
                case 27: timeTo = field.ToString(); break;
            }
        }
    }
}

The Alphabet... The Hard Way

by in CodeSOD on

James H. writes "If you had to programatically output a list of HTML <a> tags linking to anchors for the Alphabet like this:

<a href="#A">A</a> <a href="#B">B</a> ...

My Pi Goes to Twelve

by in CodeSOD on

The value of Pi is not as 3.14159265…ish as many of us would like to believe. Legislators in Indiana once declared Pi as 3.2, 4, and 3.23. Staunch biblical mathematicians insist that it's 3. The whole thing is a mess: everyone just has to have their very own Pi.

Maciek came across yet another value for Pi while a browsing the Java Swing documentation. I suppose this variation certainly the most enterprisey I've seen...


Finite State Arg

by in CodeSOD on

"It's not every day that you come across a hand-coded, table based parser," writes Joel Davis. "That's pretty hardcore. I figured it must have been needed for checking if millions of strings were uints in some super-important inner-loop. Obviously, there had to be a reason to avoid 'strtol', 'atoi' or even 'isdigit'..."

"Imagine my disappointment when I realized this was part of an argv parser."


Boolean Integers

by in CodeSOD on

Pop quiz, hot shot. There are seven different true/false flags. You have only a single integer to represent them. What do you do? What do you do?

When Stephan E's predecessor was faced with this problem, he knew exactly how to handle things. He used the integer to store a bit pattern. Of sorts. Well... kinda...


Pretty Simple

by in CodeSOD on

"It should be pretty simple," David M naïvely stated, "just look in the Agent_ProductLines table, right?"

"Uhhh," David's coworker, James, replied in a slightly condescending tone, "no." David was starting to get used to such responses. Nothing in his new job was "pretty simple" to simple to do.