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.

Nov 2008

nice_num, mean_programmer

by in CodeSOD on

When Timothy stumbled across a function that had a vague name, no comments, and variables, he took a few minutes to try to break it down in his head.

static char *nice_num(long n)
{
    int neg = 0, d = 3;
    char *buffer = prtbuf;
    int bufsize = 20;

    if (n < 0)
    {
        neg = 1;
        n = -n;
    }
    buffer += bufsize;
    *--buffer = '\0';

    do
    {
        *--buffer = '0' + (n % 10);
        n /= 10;
        if (--d == 0)
        {
            d = 3;
            *--buffer = ',';
        }
    }
    while (n);

    if (*buffer == ',') ++buffer;
    if (neg) *--buffer = '-';
    return buffer;
}

Choose Your Own Include File

by in CodeSOD on

It seemed like a simple request. "Please add 'MAINT-CNTRL-ADMIN' to the cost center dropdown for client #876." It was also Scott A's very first assignment, having recently started as a web developer for the B2B merchandise reseller.

The ecommerce application he was hired to maintain was ASP-based and had been developed over the course of several years by an outside consultant. During the job interview, the system looked pretty decent: it tracked orders, inventory, products, etc. The UI wasn't wonderful, but it worked.


Stupid Coding Tricks: The T-SQL Mandelbrot

by in CodeSOD on

The bar for entry into CodeSOD is pretty straight forward: professionally-developed code that elicits that certain What The— reaction. Though there have been a few exceptions over the years, generally speaking, student code, hobbyist code, and amateur code need not apply. That said, I'd like to try something a little different today. Today's example is not technically professionally-developed, it's a Stupid Coding Trick.

"So I was bored at work one day," Graeme Job explains, "and wondered, what's the most useless thing I could do with my time without actually doing anything. Then it hit me. I could use T-SQL to generate... Mandelbrot."


Keep it Simple and Stupid

by in CodeSOD on

Diego G. lives in Argentina and is working with a developer from the USA on a PHP project. Recently they were discussing the merits of handling the communication from the backend to the frontend via XML or JSON. The system used XML elsewhere already, but for the new work it looked like it'd be quicker and easier to work with JSON in the PHP pages.

The contact in the US didn't like one solution more than the other, and in the interest of getting the project done quickly, sent an email with his solution.


The Toggle that Wouldn't

by in CodeSOD on

"I work for a software development house that creates business software, maintaining legacy MFC applications," Graf writes. "We recently received an issue where a filter-toggle wouldn't switch back and forth, never changing from its default value. It's was a small utility function, rarely used, so we were a bit surprised to see it come up. Taking a quick glance at the code revealed the following:


Now I Have Two Hundred Problems

by in CodeSOD on

-rw-r--r--. If that looks familiar to you, skip this and the next paragraph.

OK, now that it's just you and me, Bill Gates, it is an example of Unix file permissions. The example given means that the user (owner) can read/write to the file, and others can only read it. Checking these permissions in Perl is simple; just use the stat method to check its mode (the file type and permissions). For example, if you wanted to check read permissions, you'd check bits 4 (owner read), 32 (group read), and 256 (other read). This is 292 in decimal, 0444 in octal. Compare that to the mode, and you'll know whether you can read the file. In code, stat($path)->mode & 0444. Easy peasy!


A Touch of Genius

by in CodeSOD on

“I recently started on a new contract,” Vedran R writes, “and the experience has been rather… interesting. The project I’ve been assigned to is a VB .NET 1.1 web application, and the code is rather… bad. Now, I know that developers make all kinds of compromises because of lack of time or experience but this is just… well, here’s some of the code.”

“Why fix the problem where it originates, it’s much simpler to handle it this way!”


If I've Said It Once, I've Said It Fifty Times

by in CodeSOD on

I can think of several ways to improve the code below from Jeff S., or at least to reduce its line count by two or three.

function submitTrap3()
{
    var n = 0;
    var f =0;
    var elementItem = "";
    if (window.event.keyCode == 13)
    {
        for (n=0;n<document.forms[0].elements.length;n++)
        {
            elementItem = document.forms[0].elements[n].name;
            if ((document.forms[0].elements[n].value != "") &&
                (elementItem == trim(document.forms[0].txtControlName.value)))
            {
                switch (elementItem)
                {
                    case "header1:SearchBox" :
                    {
                        __doPostBack('header1:goSearch','');
                        break;
                    }
                    case "Text1":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text2":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text3":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text4":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text5":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text6":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text7":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text8":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text9":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text10":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text11":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text12":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text13":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text14":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text15":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text16":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text17":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text18":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text19":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text20":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text21":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text22":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text23":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text24":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text25":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text26":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text27":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text28":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text29":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text30":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text31":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text32":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text33":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text34":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text35":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text36":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text37":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text38":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text39":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text40":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text41":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text42":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text43":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text44":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text45":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text46":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text47":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text48":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text49":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                    case "Text50":
                    {
                        window.event.returnValue=false;
                        window.event.cancel = true;
                        document.forms[0].elements[n+1].focus();
                        break;
                    }
                }
            }
        }
    }
}

Raiding the RAID

by in CodeSOD on

"I'm currently in the process of customizing the firmware for a RAID controller used in some of my company's products," jspenguin wrote, "The LCD Controller Module (LCM) is written in PHP and, as it turns out, an LCM is not quite what PHP was designed for. In addition to abusing exec() all over the place, they poorly re-implement functions that are built-in to PHP."

function strcenter($string, $len = LCM_ROW_LEN)
{
    //printf("%s\n", __FUNCTION__);
    $size = strlen($string);
    if ($size > $len) return $string;

    $divisor = ($len - $size) / 2;
    
    unset($new_str);
    for ($i = 0; $i < $divisor; $i++) {
        $new_str .= " ";
    }

    $new_str .= sprintf("%s", $string);
    
    for ($i = strlen($new_str); $i < $len; $i++) {
        $new_str .= " ";
    }
    //$test = strlen($new_str);
    //echo "len = $test\n";
    return $new_str;
}

function strleft($string, $len = LCM_ROW_LEN)
{
    //printf("%s\n", __FUNCTION__);
    $size = strlen($string);
    if ($size > $len) return $string;

    $remainder = $len - $size;
    
    unset($new_str);
    $new_str .= sprintf("%s", $string);
    
    for ($i = 0; $i < $remainder; $i++) {
        $new_str .= " ";
    }
    //$test = strlen($new_str);
    //echo "len = $test\n";
    return $new_str;
}


 function LCM_get_base_position($length, $tag=TAG_NONE)
{
    for ($i = 0; $i < $length; $i++) {
        $string .= "X";
    }
    
    switch ($tag) {
    case TAG_ENT:
    case TAG_SCROLL:
        $max = LCM_ROW_LEN - 1;
        break;
    case TAG_SCROLL_ENT:
    case TAG_UP_ENT:
    case TAG_DN_ENT:
        $max = LCM_ROW_LEN - 2;
        break;
    case TAG_NONE:
    default:
        break;
    }

    $string = strcenter($string, $max);
    //printf("%s: str = %s, max = %d, length = %d\n", __FUNCTION__, $string, $max, $length);
    $pos = strpos($string, 'X');
    
    if ($pos == false) {
        printf("%s: Failed to get postion\n", __FUNCTION__);
    } else {
        return $pos;
    }
}