S h o r t S t o r i e s

// Tales from software development

The unit-tested code has the most bugs

leave a comment »

One of the projects that I worked on last year has now been running in a live environment for 14 months. It handles around 5000 patient data updates per day and has performed without a single bug showing up. Until today…

It didn’t take long to find the code that was throwing the IndexOutOfRange exception. It was a method that attempted to parse a string containing a name into name components – forename, middlename, and surname. The puzzle was why the exception was occurring when the code had executed successfully for the past year.

I looked at the data that was being processed at the time of the exception and saw that the string containing the name actually contained just a single comma. Although this isn’t a valid name I still wanted to update the method so that it returned the correct output, a name structure with blank name components, rather than failing with an exception.

When I reviewed the code I noticed that the one assembly with any unit tests was the one where this method was implemented. There was severe time pressure on this project and some of our normal process engineering was bypassed. I’m not making any excuses for this but it happened. Unit tests were not written. Except for this one assembly (out of 18 assemblies in the project).

So, why did the one bug that’s shown up in the live environment occur in the assembly that had the unit tests ? At the very least it seems ironic. At worst, could it be inferred that writing unit tests makes code more buggy!

The answer is more prosaic. The assembly with the unit tests mostly contains utility methods used by other assemblies. This makes it easy to write unit tests for these methods because they are all public and have well defined inputs and outputs. It’s also a no-brainer that if there’s only sufficient time to write unit tests for one assembly then this one is a prime candidate because so many of the other assemblies depend on it. And, finally, I had a suspicion that if a bug was going to lurk somewhere in the code I’d written then it’d be in some of the routines in this assembly.


Written by Sea Monkey

August 27, 2010 at 8:00 am

Posted in Debugging, Development

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: