CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

Are Code Statistics Useful?

My team had a conversation about code statistics at lunch the other day, specifically about Cyclomatic Complexity numbers.  The gist of the conversation was whether or not it was important or useful to regularly run these metrics on your code.  My coworker's point, which I *mostly* agree with, is that he would refactor a class well before it got to a high CC number by simple inspection.  As in, he doesn't need a tool to "know" when a class is getting too big or taking on too many responsibilities. 

I would agree completely (and I think you should be able to spot bad code visually anyway), except for the old parable about putting a frog in boiling water.  If you drop a frog into boiling water he jumps right out.  If you put a frog in cool water, then gradually heat the water to boiling, he won't jump out.  Classes can be the same way as they slowly accrete yet one more function.  A CC number is an awfully quick warning that you're going out of bounds.  A run of NDepends onto StructureMap pretty well confirmed to me some weak spots in the class structure and spurred me to make some refactorings that cleaned up the internal structure.

Frank Kelly has a post that pretty well expresses my opinions on metrics.  But in brief, even though I routine forget to run NDepends, I think:

  • Metrics should only inform, never take the place of a constant attention to code structure and visual awareness of that structure
  • Metrics are very useful when you inherit someone else's codebase
- btw, this post and conversation was spawned by a codebase I was looking at (no names) that had some monster classes.  When I ran some CC numbers I came up with a half dozen classes over 200 in Cyclomatic Complexity.  20 is the rule of thumb for the upper threshold of a class, just in case you're not familiar with the CC number.  200 screams for refactoring.
 


Comments

Raymond Lewallen said:

500 million?  Is that even possible?  I couldn't do that if I tried.

I actualy use metrics quite often, specifially the CC you talked about.  Other metrics I use often are coupling, abstractness and stability.  While usually this types of metrics are easy enough to eyeball and notice just by looking at the code, I like to keep the metrics at hand just so I can look over them.  This is why NDepend is part of CI process on each build.

A little bit I wrote on using and applying those metrics I mentioned is here:  http://codebetter.com/blogs/raymond.lewallen/archive/2005/07/15/129265.aspx

# December 29, 2006 9:29 AM

Brian Schmitt said:

Not sure if you have checked it out, but there is a free Metrics Plugin for the DXCore (Coderush) that will display the metrics for a routine next to it. It also now ships with Coderush if you have it installed. It does it only for the Method level and not an entire class, but it will give you a quick idea of any trouble spots...

# December 29, 2006 10:34 AM

Raymond Lewallen said:

Brian, yeah the CC number right there next to the method is nice to have so you can always see it.  I wish it had more metrics available, but for on-the-fly stuff, it would really slow stuff down if you incorporated more metrics.  Also, a lot of metrics requred an evaluation of the entire solution, especially at the assembly level, hence the reason DxCore only supplies method level metrics.

# December 29, 2006 10:55 AM

Paul Rayner said:

Reminds me of something my Mathematics professor used to say in my stats class:

"Statistics should be used like a drunk uses a lamp post, for illumination and support"

# December 29, 2006 6:01 PM

From the software development trenches said:

I've been wanting to do a post about code metrics for quite a while now - mostly to organize my thoughts

# January 2, 2007 3:10 PM

Eric McVicker said:

I've been wondering if I could use metrics such as CC to "sell" refactoring some.  Seems like having some numbers to back a reason to change some code would appeal to some managers who are not engineers.  It sounds better when you present some numbers that you can say are linked to code stability, performance, reuse or maintenance rather than "I changed the code because it is better this way".

# January 15, 2007 5:01 PM

Annerose said:

These comments have been invaluable to me as is this whole site. I thank you for your comment.

# July 10, 2007 2:40 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP