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.

Jan 2014

Thanks Bill!

by in CodeSOD on

Alan’s coworker, Bill, had a bumper sticker on the back of his desk chair: Do not meddle in the affairs of wizards, for they are subtle and quick to anger. Bill acted the part, if all wizards looked homeless and didn’t shower for days on end. But Bill didn’t tolerate any criticism of his skills as a DBA, whether from Alan or anyone else at the company.

“Why would you use a stored procedure for that?” he’d storm. “Do you know how long that would take to execute?”


But the Tests Prove it Works Correctly!

by in CodeSOD on

I. G. wrote about an incident that caused him to nearly give himself a concussion from a *headdesk* moment. A newly developed system was meticulously designed, coded and tested to obscene levels; all appeared well.

Unfortunately, upon deployment, it began acting erratically, returning incorrect results from numerous database queries. After many debugging sessions and code walkthroughs, it was discovered that the developers had used the following pattern for all the database DAO tests:


The Temporal Countdown Timer

by in CodeSOD on

Most modern languages have the concept of letting your code sleep for a specified period of time. This is useful when you want to perform a particular task at some point down the road. It's usually implemented such that your program requests to yield control for a certain number of milliseconds. The OS will usually guarantee that your code will remain in blissful slumber for at least that amount of time, plus whatever random delay is imposed by multitasking and OS overhead.

Some folks use sleep intervals to allow for periodic updating of some tally, or perhaps a clock. Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.


Income

by in CodeSOD on

While checking the local news, Sarat discovered an “income ranker” widget on a news site. He put in his monthly income, and discovered that he was in the top 0.08% richest households in India. Surprised to discover that he was a gazillionaire, Sarat took a look at the JavaScript code which ran the widget.

The function definition started with function submitmyform(), which already looked pretty bad. Sarat looked for the block responsible for calculating the result, and found this charming anti-pattern:


Just Because It's Ruby, Doesn't Mean It's a Gem

by in CodeSOD on

There is no question that, in the right situation, Ruby on Rails is a great productivity multiplier. An anti-TDWTF, if you will. When used correctly, a highly-quality web site can be created with much great speed than more traditional coding methods.

Still, as Frederik was well aware, not every Ruby application is created equal. Frederik is a local Ruby expert and, as such, is frequently contacted by clients who were in just a little bit in over their head when it came to Rails. Much of Ruby's power comes from its use of convention over configuration. But just because there are reasonable defaults, does not mean that developers actually have to use them, right?


Throttling Throughput

by in CodeSOD on

The folks at Big Corp Inc. like to do things in very formal ways. All code is officially peer reviewed. Important code for key aspects of the system is reviewed by a manager. Only blessed tools may be used. The environment is to be pristine - regardless of cost. Following the rules takes precedence over efficiency.

David R. reported that in one particular system, the data was coming in way too fast. The message consumer was spawning a background thread for each inbound message. As volume rose, the number of threads that got spawned exceeded the capacity of the system, and NullPointerExceptions got thrown around like siding in a hurricane. The Master Architect decided that this problem was so important that he would fix it himself. Of course, since he was the apex predator, nobody would (could) review his code:


Universal Login System

by in CodeSOD on

It wasn't every day at DroneDeliveries, as Antoine knew, that a user complained about having too much access to their e-commerce account.

"I swear, I'm not making this up!" she said over the phone. The user had logged in to check her recent order status. "My email address is [email protected], but when I typed [email protected], it logged me in!"


Dropped Catch

by in CodeSOD on

Alex still has some VB6 code lurking in his environment. Like too much VB6 code, it’s littered with On Error Resume Next statements, which allow lazy programmers to simply ignore errors. It’s an easy way to make crash-proof applications in VB6.

One of their database programmers decided to compete with that anti-pattern . This programmer wrote a huge pile of database triggers which looked like this: