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 2012

Self Documenting

by in CodeSOD on

"A little while back, someone introduced the concept of 'self-documenting' code to our team," writes Ryan L. "It was certainly a step forward, but it's somehow taken us two steps backwards. Consider, for example, the following code from an MVC controller."

if (TheFormIsInvalid()) return View("Index", form);

... snip ...

private bool TheFormIsInvalid()
{
    return ModelState.IsValid == false;
}

The Enterprise User Agent

by in CodeSOD on

"When the job ad specified that an 'enterprise' Senior Java Developer was needed, I knew I'd be in a treat for some enterprisey goodness," writes Chris B, "but what I didn't expect was the User Agent Detection Generation Framework. Here's what the API for this framework looks like."


get_words_from_a_number_which_is_passed_as_a_perimeter_into_this_function

by in CodeSOD on

"As part of my new role for a software development firm," writes David Mayer, "I was assigned to sift through some code looking for bugs in the dictionary and spellchecking system. That's how I came across a function called get_words_from_a_number_which_is_passed_as_a_perimeter_into_this_function."

"One definition of the word perimeter is 'the length of a line', which seems ironic given that all the variables used are obscene in length. Incidentally, one such perimeter defines 'pass-a-number' which indicates to the function that the second perimeter will be a number. But given this function converts numbers to words, I fail to see why you would 'pass-a-letter'. "


Securely Random Strings

by in CodeSOD on

"Working through a pretty ugly project, I came across this C# beauty," Matt B writes, "at first, I was unsure of what it did. I was utterly perplexed until I took a second look at the function name. "

"It turns out, it produces a random string of letters. It was used to create a random ID for some URLs. Apparently, this developer was too proud for base 64 encoding, MD5, or some good old ASCII conversion."


Authenticate or Math

by in CodeSOD on

"I was recently assigned to work on a project that had been abandoned by its developer," wrote George Z. "After I checked-out the code for the first time, I started sifting through hundreds of lines of bad syntax, and bad practices."

"After a good hour or two looking I came across this little gem."


Roman Enumeration

by in CodeSOD on

"At my company, the powers-that-be determined that, because we rejected a lot of job candidates, my group was ineffective at hiring new employees,"Kendall writes, "thus, the responsibility of hiring new developers was shifted to a group much more proficient at hiring: human resources."

"That has been going about as well as you might expect, and to make a long story short, we were told to handle any 'knowledge gaps' with training. And thus, one of the very first training jobs I give to new employees is to develop a method that translates Roman Numbers to Decimal Numbers. Most struggle with the challenge, but one new hire actually managed to solve the problem: