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.

Oct 2013

Remember, Remember the Thirty-Third of November

by in CodeSOD on

Some say that time is nothing but an illusion. The degree to which some software developers struggle with times and dates certainly suggests mysterious and unknowable forces swirling beyond the brink of human understanding. Consider this code that Ian found while pruning an old application. It's meant to provide the correct suffix for any given day of the month:

string num = "th";

int day = Convert.ToInt16(DateTime.Now.ToString("dd"));
switch(day)
{
    case 1:
        num = "st";
        break;
    case 21:
        num = "st";
        break;
    case 31:
        num = "st";
        break;
    case 2:
        num = "nd";
        break;
    case 22:
        num = "nd";
        break;
    case 3:
        num = "rd";
        break;
    case 33:
        num = "rd";
        break;
    default:
        num = "th";
        break;
}

Decode("
")

by in CodeSOD on

A quick and easy way to generate WTF code is to find some deceptively simple problem that’s been well solved, and reimplement a naive solution to the problem.

Ryan found this attempt to handle HTML encoding of key characters. This code is VB.NET, which is important to keep in mind, since the Framework provides a variety of methods to do this correctly.


A Scrubbing Bubble

by in CodeSOD on

Matt was thrilled.

Finally, at long last, he had received the opportunity to rewrite one of the company's most overly complicated, fault intolerant, and important services. Over the years, it had achieved a "widowmaker" status where it was famous for crashing whenever it encountered one bit of uncommon (like accented characters) or mis-keyed data, often at the most busiest periods. There was talk among the teams for years of re-engineering the process but, because of its reputation, the code was deemed too gnarly and therefore too costly to fix...until now.


Which Would YOU Rather Support?

by in CodeSOD on

To populate a temporary database table, most of us would write something like this:

-- Assume table TempData exists
Truncate table TempData;
Insert Into TempData
Select ... From ... Where id = 1234 and Flag Is Not NULL;

The Senior Format

by in CodeSOD on

Alex W. used to work with a “senior” C# developer. This “senior” developer’s resume proudly proclaimed that he had 15 years of experience developing in C#.

That “senior” developer has long-since moved on to more senior pastures , but he left behind code like this:


83.3% Uptime

by in CodeSOD on

It was the klaxon noise that Jacky hated the most. If there was even a whisper of the word “hacked,” Mr. Cullen would sound the alarm.

“All-hands meeting, boardroom, right now,” Mr. Cullen said. He left the klaxon on as the employees of Red Feather assembled in the boardroom. “The webserver,” he said gravely, “is no longer responsive. We’re being hacked as we speak.”


854

by in CodeSOD on
Troy, lead developer at IniTechMobile, should have known better.

The company was moments away from launching an Android port of their popular app, WereWolfTasks. The task management app plays a video of a transforming werewolf if the user doesn’t complete his to-do list before a full moon. Because IniTechMobile was an iOS shop, they outsourced the Android port to VampMobile, an eastern European company that specialized in iOS to Android ports.

The app launched on Google Play. Almost immediately, complaints flooded Support’s ticket log. "The werewolf video is all smushed when I rotate my phone!" "My tasks keep getting erased every time my phone restarts!" "Why are the tooltips all in Romanian?"