Blast from the Past: WTF^2

I subscribe to The Daily WTF, which is a blog in which programmers come together to share funny/awful experiences with coding related things they’ve encountered. Some of the time it’s talking about hellish job experiences in which management had a really authoritarian attitude mixed with zero technical knowledge, and often times it’s a programmer who is looking at code someone else wrote and saying “WTF?” 

I was reading this article one day:

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.”

I knew to be a little suspicious when I read the author’s first paragraph, because he said that although you could use NAND gates for all your logic gates, it’s dumb because you can just use the other respective logical gates. I got suspicious at that because apparently the author didn’t realize that our chips are made completely of NAND gates, because they use fewer transistors and it’s cheaper to optimize a chip when all the gates are the same. 

And as I looked at the code, the lazy part of me did indeed think “well, you could have just used a switch statement,” which made me also question the guy’s ability to code because although he noticed this code looked weird, he failed to see the most elegant solution for this. 

Looking at the code further (and reading a comment which confirmed my suspicions), I realized that the guy who wrote the article complaining about this other guy’s code is a moron. This code might not look pretty (and break statements should indeed cause a red flag in many cases), but this is optimized code that is closely mimicking the fastest way to get through that code. The only further optimization that you can do past that (which the programmer might have done, I don’t know the context) is order the cases so that you encounter the most likely case first, because you’re making use of short circuiting (short circuiting basically means you quit once you have found what you’re looking for).

One response to “Blast from the Past: WTF^2”

  1. Christopher says:

    Lol Aaron… not going to lie. I read this and had a complete WTF moment indeed.

Leave a Reply

Your email address will not be published. Required fields are marked *

*