"I had a professor once who said that given enough NAND gates, he could rule the world," writes Rob B. "This was a roundabout way of saying that, using a whole bunch of NAND gates, you could create the function of any other logic gate. You shouldn't, because the other logic gates exist and it would be hugely wasteful to use NAND gates to do the same thing, but it can be done. It turns out this applies to code as well."

"We got some utterly garbage C++ code from a subcontractor. The error-to-lines ratio was amazingly high, and there were a lot of things to hate about it (like having one global function to get bits from a binary value which didn't work, and several different localized one-off solutions which did work). My main WTF moment, however, was the following."

while(true)
{
    if(mainType == 7)
    {
        subType = 4;
        break;
    }

    if(mainType == 9)
    {
        subType = 6;
        break;
    }

    if(mainType == 11)
    {
        subType = 9;
        break;
    }

    break;
}

"Just look at that for a minute," Rob continues, "I spent a lot of time thinking about this. The while loop is only there so that the break can be used to jump over code. I've never seen an unconditional break at the end of a loop before, but there's a first time for everything."

Rob added, "all I can figure is that the developer honestly didn't know that there is such a thing as 'else if'. But he did know about 'if', 'while', and 'break', so he cobbled together an 'else if' in the most ridiculous way possible."

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