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.

Oct 2014

The Beginning of the Zend

by in CodeSOD on

Karol found a program that needs to look at a timestamp, and determine if that timestamp is before or after an expiration date. The code that was handling this looked like this:

public function _isSmsCodeExpired($id)
    {

        $genDateStr = $this->db()->query('SELECT date FROM table')->fetchColumn();

        if (empty($genDateStr))
        {
            return true;
        }
        
        $expireDateArr = array();
        $intervalSec = 120;

        $genDataTmp = explode(' ', $genDateStr);
        $genDataArr = explode('-', $genDataTmp[0]);

        $expireDateArr['year'] = $genDataArr[0];
        $expireDateArr['month'] = $genDataArr[1];
        $expireDateArr['day'] = $genDataArr[2];

        $genDataArr = explode(':', $genDataTmp[1]);

        $expireDateArr['hour'] = $genDataArr[0];
        $expireDateArr['minute'] = $genDataArr[1];
        $expireDateArr['second'] = substr($genDataArr[2], 0, 2);

        $intervalMin = (int) $intervalSec / 60;
        $intervalSec = (int) $intervalSec - ( $intervalMin * 60 );

        $expireDateArr['second'] += $intervalSec;
        $expireDateArr['minute'] += $intervalMin;

        $expireDateArr['second'] += $intervalSec;
        if ($expireDateArr['second'] > 60)
        {
            $expireDateArr['minute'] += 1;
            $expireDateArr['second'] = $expireDateArr['second'] - 60;
        }

        if ($expireDateArr['minute'] > 60)
        {
            $expireDateArr['hour'] += 1;
            $expireDateArr['minute'] = $expireDateArr['minute'] - 60;
        }

        if ($expireDateArr['hour'] > 24)
        {
            $expireDateArr['day'] += 1;
            $expireDateArr['hour'] = $expireDateArr['hour'] - 24;
        }

        $daysInMonth = date("t", strtotime($expireDateArr['year'] . "-" . $expireDateArr['month'] . "-01"));

        if ($expireDateArr['day'] > $daysInMonth)
        {
            $expireDateArr['month'] += 1;
            $expireDateArr['day'] = $expireDateArr['day'] - $daysInMonth;
        }

        if ($expireDateArr['month'] > 12)
        {
            $expireDateArr['year'] += 1;
        }


        $expireDate = new Zend_Date($expireDateArr);
        $now = new Zend_Date();

        if ($now->isEarlier($expireDate))
            return false;
        else
            return true;
    }

Is It Safer to Use Numbers?

by in CodeSOD on

Mac didn't know anything about how the JavaScript on the search page worked, and he wasn't that great at CSS styling, but that didn't matter. He had his orders. As part of the latest round of enhancements, the front-end developer had added another search parameter which would be passed via the regular search URL, and the back end needed to be adjusted to accomodate. (You know... instead of 'http://initrode.com/search?a=xxx&b=yyy' it now was 'http://initrode.com/search?a=xxx&b=yyy&c=zzz'.)

No problem. Mac made his tweak in the code and ran a quick test...which failed instantly in a spectacular way. "WTF? It's a parameter. Must be already used..." he thought, but nope.


Parallel SQL Queries

by in CodeSOD on

Daniele worked at a pharmaceutical firm that had an old web application that allowed commercial customers to look up information. Since the data was quite complicated, there were numerous fields that needed to be queried in order to populate the form.

Unfortunately, as the amount of data in the system grew, the time to load the form grew as well. And grew. And grew.


Line by Line

by in CodeSOD on

In the bowels of a business unit, a director got a great deal on a third party software package. He bought it, without talking to corporate IT, and then was upset when it couldn’t gracefully integrate with any of the corporate IT assets. Eager to throw good money after bad, the director hired his nephew’s consultancy to build an integration tool to make his new toy work.

A few months later, the users complained about performance, and somehow, fixing this thing became Jeff’s problem. The process was simple enough: slurp enterprise data out of a text file, and pass the data on to the third-party tool. It didn’t take Jeff long to figure out why it performed poorly:


We Don't Need no Stinking Elses

by in CodeSOD on

We've all seen it before. I dare say we've all been a party to it.

First, we look at a block of code that needs to be modified. Then we determine which criteria needs to be added to address the case that we've been tasked to implement. Next, we add the condition and walk away before the waft from the code smell reaches our nostrils. Over time, a monstrosity like the following arises from the depths. Not something that couldn't be greatly improved by some fava beans and a nice refactor. I'm sure the unit tests that cover your production code base will ensure that your refactoring was successful. Right? Right??


IP Address Denial

by in CodeSOD on

While trying to assign an address from his framed /29 route to the internal side of his Linksys AM300 router, Phillip S. received an unexpected "Local IP Address is not valid" error.

Being a web script error, Phillip correctly figured that he could dig into what condition would make the address come up as invalid. Turns out, his problem was that the router considers any IP address starting with 115 to be invalid, but Phillip found that there were a few other instances of hard coding and magic number abuse. On a positive note, at least it's not device-side code.


Enterprise GUID

by in CodeSOD on

Jonathon recently got a new co-worker who is an enterprise systems developer, with an emphasis on enterprise. For an enterprise-level WTF, today’s code is short, but it packs itself up with everything it could do wrong.

using System;

namespace Business.Common.Services
{
    /// <summary>
    /// Guid service.
    /// </summary>
    public class GuidService : IGuidService
    {
        /// <summary>
        /// Initializes a new instance of the System.Guid structure.
        /// </summary>
        /// <returns>A new GUID object.</returns>
        public Guid NewGuid()
        {
            return Guid.NewGuid();
        }
    }
}