Tuesday, November 18, 2008

My Unified Theory of Bugs

by Miško Hevery

I think of bugs as being classified into three fundamental kinds of bugs.

  • Logical: Logical bug is the most common and classical "bug." This is your "if"s, "loop"s, and other logic in your code. It is by far the most common kind of bug in an application. (Think: it does the wrong thing)

  • Wiring: Wiring bug is when two different objects are miswired. For example wiring the first-name to the last-name field. It could also mean that the output of one object is not what the input of the next object expects. (Think: Data gets clobbered in process to where it is needed.)

  • Rendering: Rendering bug is when the output (typical some UI or a report) does not look right. The key here is that it takes a human to determine what "right" is. (Think: it "looks" wrong)


NOTE: A word of caution. Some developers think that since they are building UI everything is a rendering bug! A rendering bug would be that the button text overlaps with the button border. If you click the button and the wrong thing happens than it is either because you wired it wrong (wiring problem) or your logic is wrong (a logical bug). Rendering bugs are rare.

Typical Application Distribution (without Testability in Mind)

The first thing to notice about these three bug types is that the probability is not evenly distributed. Not only is the probability not even, but the cost of finding and fixing them is different. (I am sure you know this from experience). My experience from building web-apps tells me that the Logical bugs are by far the most common, followed by wiring and finally rendering bugs.



Cost of Finding the Bug

Logical bugs are notoriously hard to find. This is because they only show up when the right set of input conditions are present and finding that magical set of inputs or reproducing it tends to be hard. On the other hand wiring bugs are much easier to spot since the wiring of the application is mostly fixed. So if you made a wiring error, it will show up every time you execute that code, for the most part independent of input conditions. Finally, the rendering bugs are the easiest. You simply look at the page and quickly spot that something "looks" off.

Cost of Fixing the Bug

Our experience also tells us how hard it is to fix things. A logical bug is hard to fix, since you need to understand all of the code paths before you know what is wrong and can create a solution. Once the solution is created, it is really hard to be sure that we did not break the existing functionality. Wiring problems are much simpler, since they either manifest themselves with an exception or data in wrong location. Finally rendering bugs are easy since you "look" at the page and immediately know what went wrong and how to fix it. The reason it is easy to fix is that we design our application knowing that rendering will be something which will be constantly changing.




























LogicalWiringRendering
Probability of OccurrenceHighMediumLow
Difficulty of DiscoveringDifficultEasyTrivial
Cost of FixingHigh CostMediumLow

How does testability change the distribution?

It turns out that testable code has effect on the distribution of the bugs. Testable code needs:


The result of all of this is that the number of wiring bugs are significantly reduced. (So as a percentage we gain Logical Bugs. However total number of bugs is decreased.)



The interesting thing to notice is that you can get benefit from testable code without writing any tests. Testable code is better code! (When I hear people say that they sacrificed "good" code for testability, I know that they don't really understand testable-code.)

We Like Writing Unit-Tests

Unit-tests give you greatest bang for the buck. A unit test focuses on the most common bugs, hardest to track down and hardest to fix. And a unit-test forces you to write testable code which indirectly helps with wiring bugs. As a result when writing automated tests for your application we want to overwhelmingly focus on unit test. Unit-tests are tests which focus on the logic and focus on one class/method at a time.

  • Unit-tests focus on the logical bugs. Unit tests focus on your "if"s and "loop"s, a Focused unit-test does not directly check the wiring. (and certainly not rendering)

  • Unit-test are focused on a single CUT (class-under-test). This is important, since you want to make sure that unit-tests will not get in the way of future refactoring. Unit-tests should HELP refactoring not PREVENT refactorings. (Again, when I hear people say that tests prevent refactorings, I know that they have not understood what unit-tests are)

  • Unit-tests do not directly prove that wiring is OK. They do so only indirectly by forcing you to write more testable code.

  • Functional tests verify wiring, however there is a trade-off. You "may" have hard time refactoring if you have too many functional test OR, if you mix functional and logical tests.


Managing Your Bugs

I like to think of tests as bug management. (with the goal of bug free) Not all types of errors are equally likley, therefore I pick my battles of which tests I focus on. I find that I love unit-tests. But they need to be focused! Once a test starts testing a lot of classes in a single pass I may enjoy high coverage, but it is really hard to figure out what is going on when the test is red. It also may hinder refactorings. I tend to go very easy on Functional tests. A single test to prove that things are wired together is good enough to me.

I find that a lot of people claim that they write unit-tests, but upon closer inspection it is a mix of functional (wiring) and unit (logic) test. This happens becuase people wirte tests after code, and therefore the code is not testable. Hard to test code tends to create mockeries. (A mockery is a test which has lots of mocks, and mocks returning other mocks in order to execute the desired code) The result of a mockery is that you prove little. Your test is too high level to assert anything of interest on method level. These tests are too intimate with implementation ( the intimace comes from too many mocked interactions) making any refactorings very painful.

6 comments:

  1. Hey Misko great post, and I enjoy the blog! Is it too presumptuous of me to suggest another category of bugs? How about promiscuous references, where you accidentally share a reference and some where else someone modifies your state.

    ReplyDelete
  2. Does the "wiring" category include bugs that result from wrong assumptions about libraries or frameworks you are using? This (along with failing to take into account some of the things that can happen) is perhaps my most common kind of bug.

    ReplyDelete
  3. Can you temll me how to figure out the number of bugs?
    Anyway, I think this is a very good post... :)

    ReplyDelete
  4. > they don't really understand testable-code

    I'd love to see a blog post with a top-10 (or top-5, or top-3, whatever) guidelines to follow to produce testable code.

    I realize many of your blog posts touch on various aspects of this, but it'd be helpful to see a snapshot of the most important points.

    ReplyDelete
  5. Just do opposite of this:
    http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/

    ReplyDelete
  6. I too think there's a category missing: accidental [1] bugs.

    Race conditions, timing issues, crashes, performance problems, for example. Things that aren't essential to your program, but affect it deeply when they don't work.

    I didn't manage to classify them into either of the other buckets, but maybe you have more thoughts on this?

    Thanks,
    - Kim

    [1] In the sense of the second definition here: http://en.wiktionary.org/wiki/accidental

    ReplyDelete

The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.