One of the topics of discussion at the XP workshop
I went to on Tuesday was code metrics, and, subsequently, tools that
could be used to obtain said metrics. Here’s a quick summary of some of
the metrics we talked about:
CC (Cyclomatic Complexity) – Basically, this
is the # of decision paths in a given method. More decision paths mean
more possible points of failure and more required test scenarios.
Ca (Afferent Couplings) – This refers to the #
of types directly depending on a particular other type. Types with
lots of dependencies are considered to have high responsibility. I like
to remember this one as the Type A Coupling – as in, a type with a high
Ca has a Type A personality and is highly responsible.
Ce (Efferent Couplings) – This refers to the # of
types a particular type directly depends on. A type that depends on
lots of other types is highly dependent. I remember this one as the
Everybody Coupling - as in, a type with a high Ce depends on Everybody.
(just humor me ok)
I (Instability) – A # based on a ratio of Ca and Ce indicating an item’s resilience to change.
A (Abstractness) – A ratio of the # of abstract types to types.
D (Distance) – This is a measurement of how far from the ideal I + A something is.
LOC (lines of code) – Uh, lines of code (although, surprisingly, this can cause controversy).
For more in depth discussion go here or here, or read Robert C. Martin’s book – Agile Software Development (Quick
aside – this book is a MUST read for any developer. It is quite
honestly the best software development book I have ever read.
There. I’ve thrown down the gauntlet. If you haven’t read it, go get it
right now. Tell’em I sent ya.)
While I’d read about these different coding metrics before, talking about them and then subsequently reading this blog post by Michael Swanson
really showed me how useful these metrics might be in highlighting
areas where problems might exist in a codebase, and spurred me into
action to look into how I could integrate this analysis into our
automated build cycle.
So I set out to research different code metrics tools. My main criterion:
1. At least hit the highlights such as Cyclomatic Complexity, Afferent Coupling, Efferent Coupling, and LOC.
2. Has to be Open Source and/or Free.
3. Has to be able to integrate into an automated build
using NAnt (so, callable from the command line and able to spit out
some output in the form of xml or some such).
Raymond had mentioned a tool called NDepend. In addition, I looked at Source Monitor based on Michael’s aforementioned blog post, and one named vil. (If anybody knows of more that meet the criteria, please let me know).
Source Monitor
What I liked
– LOC
and CC analysis is based on the actual source code (rather than IL as
some of the other tools). This, in my opinion, provides a more accurate
assessment based on the original definitions of the metrics and makes
it easier to judge the results based on industry recommendations.
– Examples showing how to integrate with a build (using Ant, so I’ll have to translate these to NAnt).
– Kiviat diagrams. An excerpt from Michael:
The Kiviat diagram below is from some sample code I generated
awhile back. I can quickly tell from this diagram that I should check
to see if I've commented my code properly and take a look to see which
method(s) are driving my complexity and depth outside typical limits
(which are configurable). Fortunately, SourceMonitor makes it very easy
to find and review the complex code.

What I Didn’t Like
– It looks like it may be
fairly involved to get this going in my build script (although the Ant
example will certainly help). Also, I’m not sure if I can get the
Kiviat diagrams in my html reports yet - they may only be available in
the client app as I didn’t see them in the example – which would be a
bummer. I guess we will see. I will update this if this turns out not
to be the case.
NDepend
What I Liked:
– Packs a lot of information in.
– Nice ability to configure which assemblies are included, and which are considered framework or outside of your control.
– Some really nice graphs such as the Assemblies Abstractness vs Instability and an Assemblies Dependencies Diagram (see below).


What I Didn’t Like:
– Analysis
is based on IL rather than actual source files, so some of the numbers
are a bit inflated – particularly the one for Cyclomatic Complexity,
which the documentation suggest could be off by a factor of 2 or 3.
–
No Configurability of what metrics get output as far as I could
tell – so it’s all or nothing, which is ok, I suppose, but I’m not
sure I want to use all the metrics at this point. I would’ve liked to
have been able to specify the ones I’m interested in.
– Very little control of where the output gets sent to.
vil
What I Liked:
– Highly
configurable as to what types of things (i.e. classes, enums, structs,
methods, etc) should and should not be analyzed, and what kinds of
metrics should be output.
– Ability to filter the results based on
criteria. The documentation is a little bit sparse on
examples here, but I think this might allow someone to, say, spit
out all methods that have a CC > 10, or something like that. If so,
this could be really useful.
– A CruiseControl.NET xsl plugin has already been created for vil, along with an example of how to set it up in a NAnt script.
What I Didn’t Like:
– Again, analysis based on IL.
– I’m
not sure if I’m not using it properly or what (although I tried almost
every possible command switch combination), but when I ran the
tool against some existing code that had approximately 50–60 classes,
vil only displayed information about 4 of them. I plan to send an email
to the support address and see if I can figure out what’s going on.
I decided that each had enough positives to warrant a
shot, so I’d try to integrate each of them into my
CruiseControl.NET/NAnt build and see how it went and see which ones
were keepers. I’m about half way through this process now, so over the
next few days I will post about my experiences with each. Stay tuned.
(Also, if anybody has any examples of their own of
automated build integrations with one of these tools or others, I’d
love to see them)
(Update) See the posts detailing how to integrate each of these into CruiseControl.NET here:
NDepend
vil
SourceMonitor
Technorati tags:
Code Metrics