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.

Aug 2014

The Database Gazes Also Into You

by in CodeSOD on

When Simon asked us to consider this code from his predecessor's custom-built PHP CMS, we weren't terribly impressed:

$rs = new RecordSet("SELECT * FROM moduleData WHERE moduleID = '".$moduleID."' ORDER BY displayOrder ASC");

An Attempt at Proper JSP

by in CodeSOD on

When developers first got access to those new-fangled gadgets called computers, memory was a very precious resource. Applications were frequently written as a main controller that would load module overlays into memory, call a function, and then repeat as additional functions were called. It was a horrible way to code, but it was all we had. Unfortunately, as computers came equipped with more and more RAM, this habit of repeating the controller code in every file seems to be quite resilient...

Fast forward several decades, and Jeremy, like the rest of us at some point, was a newbie at his first position as a developer. The application that he was tasked with maintaining had been written by an engineer whose training apparently included learning basic JSP control-structures, and how to perform cut-n-pasting of code from A to B.


Misguided Optimization

by in CodeSOD on

States and their abbreviations are among my favorite kinds of data - they almost never ever change and, as such, you can hard code all that information into your app. I mean, why bother fetching it from the database every page load? That's just wasted CPU cycles.

So, I can find merit in the hard-coded approach that the below code takes that Alex E. sent our way. However, I definitely believe that it takes guts for anybody to make a claim about the efficiency of strcmp() when you perform a linear search on an ordered list.


The Constant Bomb

by in CodeSOD on

On one hand, this Java class Jim found is just another instance where somebody made constants like this:

	public static final String NO_SPACE = "";
	public static final String SINGLE_SPACE = " ";
	public static final String DOUBLE_SPACE = "  ";
	public static final String ZERO = "0";
	public static final String FLAG_Y = "Y";
	public static final String FLAG_N = "N";

Securing Input

by in CodeSOD on

We all know that many developers have difficulty in dealing with built-in concepts like dates and times, and that for and switch statements don't necessarily have to be used with each other. However, validating a piece of input is usually more straightforward. You compare what you got to what was expected.

Mathieu was tasked with migrating some Groovy scripts. While the technical migration was fairly easy, he found it necessary to rewrite certain portions of the input validation routines. For example, the task of validating the month portion of a date string seemed straightforward enough...


Day After Übermorgen

by in CodeSOD on

While working on his company's reservation manager, Stephaan stumbled upon some PHP code that calculated the date values for tomorrow ($morgen) and the day after tomorrow ($ubermorgen). Something about the code struck him as ... wrong.

``` // FORMAT DATE // detect this day and this month (without 0) $today = date("j") ; $thismonth = date("n") ; $manyday = date("t") ;


Literal Scripting

by in CodeSOD on

The HR team at Initrode were a happy bunch, casting their nets into the perpetual stream of eager undergrads from nearby WTF U. It was a summer tradition at Initrode to invite a school of juniors to get a taste of their future by spending the long, sun-drenched afternoons of their dwindling youth hunched in cubicles.

Chris was on the Dev Tools Team at Initrode, building widgets and gizmos to help his fellow developers be more productive. Since few of his colleagues were willing to unleash students on production code, the duds among the summer-student pool tended to end up on Chris's team. And that's why the intern at the center of this SOD bears the pseudonym Dudley.


Too Much of a Bad Thing

by in CodeSOD on

The question of whether you should include in-line comments in your code is a running one in the development community. To some, they are part of the process of ensuring the ongoing maintainability of a codebase. To others, comments are the spawn of satan, lower than cockroach droppings, or slightly above a Justin Bieber song.

Regardless of where you fall on this spectrum, it's reasonably certain that the following isn't considered 'acceptable'.


The Thread Mismanager

by in CodeSOD on

We've all heard of threads. No, not the stuff hanging loosely from your clothes. I mean threads, as in multitasking. Most modern languages have all sorts of nifty facilities that allow you to create, manipulate and destroy them at will and with minimal effort. There are even abstractions that will manage a set of threads for you, so that you can spawn a bunch of tasks, and let them tell you when they're done. You can synchronize them yourself. You can put up cyclic barriers to make them all wait at a specific point in the code. You can make them return a value when they're done. Or you can just spawn them and let them run all by their lonesome. Of course, not everyone trusts the built-in facilities... Now you might expect this sort of thing from Joe Offshore, but not from certain huge, blue companies.

Baron inherited something written by such a huge, blue company. Basically, it monitors a database, does some work for each changed record and then deletes the record from the database. Unfortunately, this little beast suffers from horrific performance problems and requires frequent server reboots to get things running again.