Every now and then, you’ll come across some code that’s just like a car crash. The twisted, mangled-up kind of crash where the car’s on its side – possibly even on fire – and you can’t help but stare at the wreck with a mix of disgust and interest. Can it be fixed? Hmm, maybe something can be salvaged from the mess. Sheesh, I hope no one died as a result of this.

Samara N recently came across some car crash code: there were virtually no comments and the documentation simply read “handles time.” It came as no surprise that the code was written by Jared, also known as the Lead Developer’s sister’s daughter’s boyfriend. As everyone knows, nepotism rarely generates anything good, and in this case it generated something god-awful that no one dared to touch.

Jared’s time library was built around the traditional time.h library and used only time(NULL) calculations. Everyone on the team knew that Jared’s time system was a bit broken, but they still used it since parts of it actually worked. That is, until February 29 of last year, when the system came cashing to a halt. That’s when Samara went in to try to fix things.

The first line of time_help.h that greeted Samara seemed innocent enough.

#include <time.h> // Includes base header time.

And then there were incarnations like this...


And this, which made it pretty clear that Jared wasn't very inventive with loops.

int month_function()
    int days = DAY;
    if ( days <= 31 )
        return 1;
    } else if ( days <= FEBLENGTH+31 )
        return 2;
    } else if ( days <= FEBLENGTH+31+31)
        return 3;
    } else if ( days <= FEBLENGTH+31+31+30 )
        return 4;
    } else if ( days <= FEBLENGTH+31+31+30+31 )
        return 5;
    } else if ( days <= FEBLENGTH+31+31+30+31+30 )
        return 6;
    } else if ( days <= FEBLENGTH+31+31+30+31+30+31)
        return 7;
    } else if ( days <= FEBLENGTH+31+31+30+31+30+31+30)
        return 8;
    } else if ( days <= FEBLENGTH+31+31+30+31+30+31+30+31)
        return 9;
    } else if ( days <= FEBLENGTH+31+31+30+31+30+31+30+31+30)
        return 10;
    } else if ( days <= FEBLENGTH+31+31+30+31+30+31+30+31+30+31)
        return 11;
    } else if ( days <= FEBLENGTH+31+31+30+31+30+31+30+31+30+31+30)
        return 12;
    } else
        return -1;

Fortunately, by the time the February 29 bug was discovered, reported, and assigned, the day had almost passed, leaving Samara to mark the bug report as “fix later.” And hopefully, by the time the next February 29 rolls around, someone else will be there to fix it.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!