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 2014

The Fix

by in CodeSOD on

As one of the more experienced DBAs at RBC (Really Big Corporation, Inc), Marle was frequently called upon to review changes being proposed by her younger, less worldly-wise colleagues. This includes not just "pure" DBAs but also DBA wannabes (otherwise known as developers). Marle had no problem with this part of her job. People had mentored her when she was just getting started and paying back was just good karma. It was also, occasionally, the source for a really good WTF.

On a Tuesday morning, the following stored procedure to review came across her desk.


Bourne to be While

by in CodeSOD on

Alvaro M. used to work as a contractor for large US store chain where many things were accomplished via shell scripts. The majority of which were written by one guy who was the "expert" on scripting - a veritable scripting guru.

Scripts doing infinite loops and then sleeping for an hour were often the norm. The reasoning behind these being that "cron used too much server resources!" There was even a server where every user was allowed to put their scripts, that was painfully slow to use, partially because at any time you could see a couple hundred instances of 'sleep'...for example:


Chinese, Korean Japanese - It's All the Same

by in CodeSOD on

Matt H. and company recently added support for Cyrillic script to their PDF invoice generator when they discovered that none of the characters would print. The script used DOMPDF to convert the HTML invoices to PDF, and font handling across scripts can get a bit hairy, so it was not really a surprise. However, as he was digging through the code that generated the invoices, he found this little gem:

<?php
...
$body = mb_convert_encoding($body, 'HTML-ENTITIES', 'UTF-8'); 
 $body = preg_replace_callback('/(&#)([0-9]{4,})(;)/', function($matches) { 
 $code = $matches[2]; 
 if ($code<=19968 && $code>=40895) return $matches[0]; // not CJK 
    return '<span style="font-family:kochi-gothic">'.$matches[0].'</spank>'; 
 },$body); 
 $body = mb_convert_encoding($body, 'UTF-8', 'HTML-ENTITIES');
...
?>

Test Driven Delay

by in CodeSOD on

The application that Motumbo works on has huge codebase and very few tests. The majority of these exist to test integration and interact with the DB or other external systems - they either print something out or rely on exceptions being thrown.

The rest, didn't actually work (e.g. reference local paths) and weren't executed in the main build. Instead, there's a separate build which runs them and succeeds even if they fail. Thankfully, this situation was correctly recognized as being ridiculous and the decision was made to clean house and either fix or remove the useless, broken ones.


Caching Debugs

by in CodeSOD on

Christian's team was writing a time-management application in Java and utilizing a third party library. The application needed to be up and running 24/7.

A few days after a recent release, a customer called in a problem report complaining that the JVM heap was growing in size over time until it crashed with an out-of-memory error.


Some Useful Bits

by in CodeSOD on

A junior developer recently left the firm that Graham works for. In an act of great kindness and generosity, before officially departing, he sent to the developer who had inherited his work a file titled 'Useful_Bits.txt' - some code that he had lovingly written and could potentially be reused.

The file included, among many other things, the below gems. The last one had reportedly taken ages to write but the departing dev got upset when it was pointed out that you could do it in one line by overloading ToString() or using string.Format()...


Knowing is Half the Battle

by in CodeSOD on

At Initrode, their main business is large-scale data warehousing. Their products are used by the biggest companies in the world, and to keep up with demand, naturally, they have hundreds of rock star developers churning out incredible solutions.

They also have Commander Joe.


Exceptional Recursion

by in CodeSOD on

Learning to handle exceptions properly, knowing when to catch, and when to throw them, is an art. Knowing when it’s a case to retry or to abort a process requires a deep understanding of the problem you’re going to solve.

This code, which Adam found, is a pretty bad example of what to do:


Screwing Up the Screw Up

by in CodeSOD on

Highly Paid Consultants™ should be knowledgeable in their claimed area of expertise. One would assume that any consultant would have a basic understanding of what passing parameters to a function is for and how to utilize that concept in their chosen programming language.

One would be wrong.