We've recently discussed how bit masks can confuse people. Argle's in a position to inherit some of that confused code.

In this case, Argle's company inherited some NodeJS code from a customer who was very upset with the previous development team they had hired. It was a mix of NodeJS with some custom hardware involved.

Like many of us, the first thing Argle's team did was just pull up the code and skim the documentation. It seemed thorough and complete. But it wasn't until they started looking at the implementation that they started to see the true horrors.

Someone had reimplemented all of the bitwise functions as methods. And the core pattern revolved around the bitTest function:

function bitTest( value, bit ) {
    let temp = value.toString(2);
    temp = temp.split('').reverse().join('');
    let i;
    for( i=temp.length; i<32; i++ )
        temp += '0';
    if ( temp[bit] == '1' )
        return true;
    else
        return false;
}

This turns an integer into a string of its binary representation, splits it into an array to reverse the array to rejoin it into an array (gotta think about endianness!), and then pads the string out to 32 characters long, all so that it can finally ask if temp[bit] == '1' is true.

But think about the readability benefits! Instead of writing the cryptic if (variable & (1 << bit)), which is nigh unreadable, you can now say if (bitTest(variable, bit)) which doesn't actually convey any more information and in fact conceals a gigantic pile of string manipulation for a very, very simple check.

I expect this code to be uploaded to NPM and turned into a microframework that ends up powering 75% of web applications by the end of the week, as a dependency-of-a-dependency-of-a-dependency.