You can't really blame Bjørn for not listening to the radio much anymore. If you'd had to spend months maintaining the in-house web application he inherited, you'd develop some negative associations of your own.

Bjørn was always baffled by the occasional complaints he'd receive from users, reporting that the filtering and categorization functions on many of the application's pages were broken. The pages displayed sets of radio buttons to allow users to select among various options, and apparently these buttons didn't always work. The problems always had hazy descriptions and, no matter what, Bjørn had never reproduced a single one.

He'd also never had a chance to look at a certain script used by the application: the mysterious custom.js. It was only when he needed to add a brand new radio-button set himself that the source of the inexplicable issues was revealed:

function fnCreateRadioButtons(aData) {  
    var prefix = Math.floor((Math.random() * 9999) + 1);
    var r = '', i, iLen = aData.length;
    r += '<div class="radio-filter"><input checked="checked" name="' + prefix + 
        '" type="radio" value="" id="' + prefix + '-all"><label for="' + 
        prefix + '-all">' + window.lbAll + '</label></div>'
    for (i = 0; i < iLen; i++) {
        r += '<div class="radio-filter"><input name="' + prefix + 
            '" type="radio" value="^' + aData[i] + '$" id="' + prefix + '-' + i + 
            '"><label for="' + prefix + '-' + i + '">' + aData[i] + '</label></div>';
    return r + '';

The function, which cobbles together HTML to produce each radio-button set on every page that needs them, has to give the set a name that is unique across all radio-button sets on the page: the way radio-button sets work, only one option from each set can be selected at any time. The script's developer, who'd fled into the night long before Bjørn's arrival, had decided a good way to do that was to assign each set a random number between one and 9999. With an average of four sets of buttons on each page, Bjørn figured, the set names would collide about every 2500 page views. Since the application was heavily-trafficked, about once a day some user would be served a broken set of radio buttons. Since Bjørn had never developed the habit of reloading a page several thousand times while attempting to reproduce a bug, he'd never experienced it.

Bjørn has since replaced the prefix generator with a page-global counter and thrown away his clock radio. He hopes not to have to think about radio buttons or randomness for a long, long time. It's too bad his new iPod Touch has that Shuffle feature...

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