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 2016

Utter Filth

by in CodeSOD on

Terrell inherited some database stored procedures. Like all good, Enterprisey stored procedures, it was written in PL/SQL. Unlike most Enterprisey procedures, it had a clear purpose: to clean your dirty inputs.

The actual problem: based on user input, the PL/SQL code needed to write a temporary file to the filesystem. Since user input is full of filthy, illicit characters, this procedure needs to clean them up.


OutputCache All The Things

by in CodeSOD on

Treasure chest color

Steam. AT&T. Marks and Spencer. Bebo. What do they all have in common? One morning, as customer support tickets rolled in at her online retail firm, it became Belle's job to find out.


The Helpful Customer

by in CodeSOD on

Sven built a PHP-based website on contract then handed it off to his customer, Bob.

“You won’t be getting a lot of support calls from me,” Bob said, “because I actually know a thing or two about PHP. I’ll be maintaining this myself.”


Reloaded Commands

by in CodeSOD on

William Heimbinger joins the ranks of developers who come to us to confess their sins. This particular sin was committed when he was but a young child of fifteen years old, which raises more questions than it answers.

Young William was writing Perl and building an anti-spam bot for IRC channels. As he wrote code, he wanted to quickly reload the module to test it, but actually learning how to reload modules looked like too much work. William decided to reinvent that wheel, using a hammer he already knew.


The Flasher

by in CodeSOD on

Michael H sends us some code that probably deserves a NSFW warning for exhibitionism. This code is a confusing bit of metaprogramming that… well, shouldn’t be allowed near schools or playgrounds.

function load_class_public($class) {
        static $classes;
        if (! $classes)
                $classes = array();
        $classname = $class . '_Publicified';
        if ($classes[$classname])
                return $classname;
        $codez = file_get_contents(PATH . '/classes/class.' . $class . '.php');
        $codez = preg_replace('/\bprivate\b/', 'public', $codez);
        $codez = preg_replace('/\bprotected\b/', 'public', $codez);
        $codez = preg_replace('/\bclass\s+' . $class . '\b/', 'class ' . $class . '_Publicified', $codez);
        $codez = preg_replace('/\<\?php/', '', $codez);
        eval($codez);
        $classes[$classname] = $classname;
        return $classname;
}

Ch-ch-ch-changes

by in CodeSOD on

Archie poked around in his company’s billing system. This was a mistake, because that system is implemented largely in PL/SQL, and only a developer made from the sternest stuff can deal with PL/SQL.

Like most PL/SQL applications, the Oracle database is the closest thing they have to version control. Each developer makes changes in the live dev environment and then hopes for the best. They don’t use TOAD or SQLPlus or any “normal” tool for making these changes- they have to use an in-house developed GUI, because that GUI tracks their changes and writes rows into a database called “VersionDB”. When they finish a patch and want to release the changes to their customer sites, they send a copy of the VersionDB and let a simple script apply all of those changes.

It works about as well as you’d expect.


Good Idea, Bad Idea

by in CodeSOD on

After years of neglecting their command line tools, Microsoft decided to try and build a grown-up set of administrative tools, and released PowerShell. Nearly a decade later, and many of their flagship services still don’t integrate neatly with PowerShell. Good Idea, Bad Idea The syntax is a bit messy, and the promises of an object-oriented shell never quite came to fruition. PowerShell is a great idea, executed poorly. For all that, it still offers certain advantages compared to the Unix family of shells, and is certainly worlds better than ol’ CMD.EXE.

Speaking of good ideas executed poorly: backups aren’t a good idea, they’re a great idea. And when Tommy took over as an Active Directory engineer, he was happy to hear that his predecessor had left behind a script that backed up all their user data on a daily basis. Tommy was significantly less happy when he saw the script.


A SASsy Import

by in CodeSOD on

After ten minutes of Mike staring at the screen unmoving, Jill sighed. As the newly minted tech lead, she knew that she had to help unwedge problems. But she also knew that Mike had been tasked with a task which should have been simple, had the project been developed in their team. Alas, it had been thrown over the wall for them to maintain, as a “reward” for cleaning up so much else.

The calculations for generating quotes for customers were, for some complex scenarios, quoting too low; they were missing some items. The originators of the quote system had used Excel, so there was probably a hard-coded range to be updated. She’d asked Mike to fix it, create a playbook for this system and create a ticket sketching out what might be needed to make the system more resilient and maintainable. Excel would still be required, for the actuaries wouldn’t hand over formulae in anything else, but perhaps some jiggling into independent sheets and just running summaries over “the whole sheet” would reduce the pain. Jill had thoroughly cautioned Mike that he did not want, at this point, to discover the paperwork and bureaucracy to change how the calculations were performed, so the only changes to be made in this first approach were to be the minimum necessary, while keeping the style and solution intact.