Anatomy of a Bug

Those who were at our last event in the Minecraft Mod room might remember this curious site we came across:


Clearly something has gone wrong here…

Apparently we created a recipe which takes no ingredients. Looks like we have a bug in our software. This actually illustrates an important lesson to learn as a developer:

All software has bugs.

Whether your software is complex, simple, hardly used, or the most popular app in the app store, it has bugs. Even the most intensive testing will not find all bugs that exist in a piece of software.

So, now that we have a bug we want to figure out why it’s happening. I tried making small changes to the code, removing code to simplify what was going on, and change working code to be more like the broken code all to try and diagnose what went wrong. Finally I came across this piece of code:

String row1 = ((input[0][0] != null) ? "a" : " ") + ((input[0][1] != null) ? "a" : " ") + ((input[0][2] != null) ? "c" : " ");
String row2 = ((input[1][0] != null) ? "d" : " ") + ((input[1][1] != null) ? "e" : " ") + ((input[1][2] != null) ? "f" : " ");
String row3 = ((input[2][0] != null) ? "g" : " ") + ((input[2][1] != null) ? "h" : " ") + ((input[2][2] != null) ? "i" : " ");

Do you see something which looks out of place? If you can say your ABC’s you might… Even though you may not know what’s going on here, (using the ternary operator as a quick way to evaluate if/then/else statements) you still may be able to spot the error

That’s right. I copied and pasted this code a number of times instead of typing it all out (refer to the lazy/efficient programmer discussion we had). I managed to not update the second “a” reference which should be a “b”. This was causing recipes which had an ingredient in the position [0][1] to not be created properly. With that simple fix in place now we can see:


Much Better!

I was curious how long this bug existed so I looked at the code’s history in GitHub and found out it has been there ever since the code was first committed, about a year ago. How could this bug have existed for so long? Well, this specific use was simply never tested.

Bugs happen! They can be frustrating, but they can also be fixed. I fixed this one and committed it to our source code with a nice message (“Fixed recipe bug (dupe ‘a’ keys)”) so that hopefully nobody will encounter this bug again. Or if they do, then can look through the history and found out how to fix it!

In the comments to this post, tell of a time you found a software bug. What happened? Could you reproduce it? Have you found any bugs in Minecraft?

And as promised, here is an excellent tutorial for getting your Minecraft Modding environment setup for Minecraft 1.7: