Dave Laribee

Sponsors

The Lounge

Advertisement

Code Coverage: What Is It Good For?

Absolutely nothing. Or so the story goes...

I had a mini-exchange with my pal Glenn Block last night on Twitter. He was recycling the somewhat old meme that code coverage is a cockamamie metric that, as he puts it, "[code coverage is] a false security blanket on quality".

Measurements (other than net profit, of course) are merely indicators. They give us insight into what we're doing, red flags that help us know when to follow up on and drill into with more qualitative and hands on tools.This is, more-or-less, the idea behind Autonomation or "automation with a human touch."

For example: while code coverage isn't proof that our testing efforts will yield higher maintainability, it does tell us a teams commitment to a test practice.

I tend to believe the most interesting indicators are composite metrics. What happens when we start thinking about combining code coverage with cyclomatic complexity? I'd like to read a (hopefully) inverse relationship over time, coverage rising while complexity falls, as a good indicator that we're doing simple design.

The important thing to remember about metrics is they are only single purpose tools which, correctly applied, gift us with little tidbits of insight. It's up to you to find meaningful and creative uses for the various metrics and their various combinations. Once you start doing that, measurements like code coverage can certainly help you achieve your immediate goals.


Posted Fri, Nov 13 2009 3:06 PM by Dave Laribee
Filed under: , , , ,

[Advertisement]

Comments

Rob wrote re: Code Coverage: What Is It Good For?
on Fri, Nov 13 2009 4:06 PM

Agreed. Code metrics merely open your eyes to what you are doing and not doing. It's then the developers' job to fix what is important.

That is the metrics only value: Insight.

Bertrand Le Roy wrote re: Code Coverage: What Is It Good For?
on Fri, Nov 13 2009 4:40 PM

Right, it's a metric. Complaining about the information it gives you is like complaining about temperature not giving you the position and velocity of all molecules. That's the point of the metric: to reduce the view on a system to a single value. It does not characterize the system, it measures something. Only by adding other metrics can you get a more real idea of the underlying system. The difficulty is to know what and how much you need to know.

Dax wrote re: Code Coverage: What Is It Good For?
on Fri, Nov 13 2009 4:46 PM

So the question is, how does one convince the FDA?

mendicant wrote re: Code Coverage: What Is It Good For?
on Fri, Nov 13 2009 10:34 PM

Hearing about this the last couple days I realized that it's not really the coverage number itself that I usually pay attention to but rather what it's doing. When it's falling I wonder what's happening. When it's rising I feel a bit more comfortable.

It also helps that I fully trust the testing capabilities of my coworkers.

Community Blogs wrote Metrics in software and physics
on Sat, Nov 14 2009 2:50 AM

Every so often, somebody points out how bad of a metric code coverage is . And of course, on its own

Steve Py wrote re: Code Coverage: What Is It Good For?
on Sat, Nov 14 2009 6:16 AM

I don't put much importance on the coverage "metric" but looking at code coverage as a whole is certainly important. Identifying areas that aren't covered is critically important, but also the number of times each element is "touched" can be equally important especially when CC is unavoidable. The metric may say that nearly every line is touched, but that certainly lends nothing to assessing whether each combination has some form of coverage.

Bad tests can spoof any effort to mine information out of coverage so it's certainly *not* enough to just say "The code is safe, the code is clean, so long as those bars are mostly green."  Personally I review as much of the code, and unit tests as time affords me.

PimpThisBlog.com wrote Code Coverage: What Is It Good For?
on Sun, Nov 15 2009 8:02 AM

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

Dan Pickett wrote re: Code Coverage: What Is It Good For?
on Sun, Nov 15 2009 11:42 AM

Like anything, I think metrics are best used and studied and moderation. When coming on to a new project, you can glean a lot of insight just by getting code coverage. It helps to diagnose problems, not resolve them.

Some people I know are perfectionists when it comes to this metric. 100% code coverage in and of itself is kind of a myth - you still have integration issues as well as potential false positives with mocks and stubs.  You described it really well when you said it can act like a "security blanket"

uberVU - social comments wrote Social comments and analytics for this post
on Sun, Nov 15 2009 4:42 PM

This post was mentioned on Twitter by laribee: Posted - Code Coverage: What Is It Good For? http://rurl.org/23m7

Nick wrote re: Code Coverage: What Is It Good For?
on Sun, Nov 15 2009 4:59 PM

To be honest, you really have to look at functional coverage rather than actual coverage made by writing tests.

People can (wrongly) engineer tests that will check down different branching points in code, but don't actually identify the fact that something should or shouldn't happen.

This is where imho, BDD is a good route to take, as it describes the functionality of a piece of code in the given a set of circumstances.

Rob wrote re: Code Coverage: What Is It Good For?
on Mon, Nov 16 2009 5:01 AM

Composite metrics are definitely the way to go. The more different ways you are able to measure your code quality the better. I've been playing with integrating NDepend and NCover into Teamcity and have come up with some nice visual dashboards:

blog.robbowley.net/.../visualising-the-internal-quality-of-software-part-1

GV Blog wrote Metrics in software and physics
on Wed, Nov 18 2009 8:00 AM

Metrics in software and physics

Gil Zilberfeld wrote re: Code Coverage: What Is It Good For?
on Wed, Nov 18 2009 9:56 AM

Hi Dave,

We do a weekly webcast on testing called "This week in testing" (learn.typemock.com/.../episode-4-animals-that-talk-back.html)

and we've mentioned this post (and the one about the cost of losing a developer).

Please come and watch, and if you like what you see, please tell other people as well.

Thanks,

Gil Zilberfeld

Typemock

Jesse Gibbs wrote re: Code Coverage: What Is It Good For?
on Tue, Nov 24 2009 11:14 AM

I second the comment by a few previous readers that code coverage tools can reveal some important insights, including:

* The 'riskiest' areas in your codebase:  A combination of low coverage and high compexity definitely warrants investigation.

* Loss of code coverage over time is an indicator that code is changing but tests are not, which can be problematic.

IMHO, much of the negativity towards code coverage metrics is due to their misuse.  The statistics can easily be used to instill a false sense of security in stakeholders who don't understand that touching a line of code w/ a test doesn't make it bug-proof.  I'd prefer to see code coverage metrics as a tool used within the development team to prioritise where to invest in new and updated unit and functional tests.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?