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 2013

Hangin' On

by in CodeSOD on

Don T. was tasked with adding the ability to process transactions with new reference data to their flagship product. After some thought, Don decided that he would create a DAO to load the new reference data from the database, and store it in another sub-cache in the singleton master-cache of the application. Further, he would grab a handle to that sub-cache and pass it around to anything that needed it. The actual processing would be done by a remote web service. There would be approximately 100 megabytes of new reference data. He wrote and tested the code. QA tested and blessed the code. It was deployed.

Within an hour, the application died with an OutOfMemoryError.


All the Pieces

by in CodeSOD on

So much bad code arises from the fact that people don’t know that there’s a better way. If a developer doesn’t know about “split” and “join” functions, they might write a cumbersome for loop to manipulate a string. If they don’t know about regexes, they might really overcomplicate some string munging . Or heck, maybe they don’t understand a for loop, and use a while instead.

Maurycy submits this PHP code. It’s hard to really understand how this particular version of the code came into being.


Try More Random

by in CodeSOD on

All of Christian R.'s fellow employees were so overloaded with criticality-1 tasks like replacing icons, fixing spelling mistakes and such that their company decided that it would be prudent to outsource new work to Experts. Management reasoned that choosing highly trained experts would ensure that the job would be done right the first time. The solution would be cost effective, efficient and make them look wise.

One particular new task was to enable two devices to be paired. This would be accomplished by having one device display a number. The user would enter the number on the other device: the two devices would be paired and the user would be logged in on both devices. Underneath the covers, both devices talked to a common server. Once paired, the devices could talk to each other (through the server).


SELECTing Valid SQL

by in CodeSOD on

“No EXECUTE-ions,” the whiteboard read. Doug always smiled at the note in the common space at Wolfram and Hart Software, but it had taken on a more sinister meaning lately.

“Use a stored procedure? No can do,” the DBA Joss said. “The codebase simply won’t support it. If your query requires anything other than SELECT, INSERT, UPDATE, OR DELETE, it fails.”


The Right Way to Find a File

by in CodeSOD on

When not at the office at StartCo Ltd, AJ raises her two kids with her husband, who works at home. A few months ago she was working long hours on a migration to a new codebase, so she made up the lost sleep on the weekends. While her kids were placated with episodes of The Muppet Show on repeat, she would doze off on the couch.

One Saturday afternoon, with the two kids enraptured by “Pigs in Space,” she took her usual nap. A few minutes later, her youngest daughter urgently shook her awake. “Mommy, Mommy! Are you awake, Mommy?”


Trust Me

by in CodeSOD on

Pritesh C. took a position working for an Architect's Architect who could out-design anything to out-do and out-perform any other system. Everything this guy ever designed and coded had more features, capabilities and brute-force performance than any similar widget ever designed by anyone else, anywhere, ever.

Way back when the system was first being designed, the Architect's Architect decreed that communication with the system was to be via JMS messaging. Additionally, there would be two queues; one for normal and one for high priority messages. Applications requiring the services of this new application would send normal priority messages for batch processing, and high priority messages for direct user-interaction. The application would monitor both queues, but let the high priority messages jump to the head of the line for faster response. Also, due to the system it was to replace being notoriously buggy and unreliable, this system had to be bullet proof. It simply could not go down.


A Long Running Process

by in CodeSOD on

A process might run for a long time, and it’s important for code to ensure that the long-running process doesn’t crash unexpectedly. Alexander found this block of code, which carefully protects against overflows:

if (_totalSeconds < ulong.MaxValue)
{  
   _totalSeconds += 1;
}  
else
{  
   _totalSeconds = 0;  
}