Forks are good. Most of the time when I eat a meal, I get by on just
a fork. They can stab, scoop, cut, and generally get the food to
my mouth without making too much of a mess. Some meals though call for
other tools such as a spoon (for soup) or a knife (for steak), or maybe
even chopsticks (for impressing family and friends - certainly not
because they do a better job than a fork or spoon, but I digress).
What does this have to do with anything you ask? Well, the way I see it, vil is the knife to NDepend’s fork.
While NDepend
is just fine for getting most of the meal from the plate to my mouth,
vil is there for the special task of cutting things up and making them
manageable. It’s a slicer and dicer. And a fairly powerful one out
that. It’s like one of those Miracle Blade knives you see on that infomercial at 2am.
See, vil has these great command line switches for creating filtered code metric queries. Let me show you what I mean.
Here’s a NAnt task for returning all methods with a Cyclomatic Complexity > 7:
And a task for returning the top 15 most complex methods:
And, here’s one that will get you all of the methods with more than 5 local variables defined:
You can sort on any field you like, return only the top or
bottom n items, limit the scope to classes or methods or even down to
just constructors, and you can pick and choose any number of
metrics in which you are interested. (see the docs for more). I told ya – Miracle Blade! And this one won’t even cost you $39.95.
As far as integrating vil into the build – compared to NDepend, this tool is much simpler to get going with. The CruiseControl.NET documentation already includes instructions, and since vil gives you plenty of command-line control over the input and output, and since there are no images to integrate, you should be able to get this up and running on your builds cleanly and quickly without much addition from me.
I will add this though…
The latest build of CCNet comes with a vil stylesheet, but it’s
pretty plain-Jane (my apologies to any readers named Jane). I elected
to go with a slightly modified version of the alternative vil
stylesheet found at the Community Contributions page for XSL Transforms (contributed
by Willie Tu). It spruced up the colors a bit and was generally more
readable, but I had to muck with it because it included some xsl
commands for sorting alphabetically, which interfered with my
already-sorted queries (get my version here: vil-alt.xsl).
The really nice thing about this stylesheet that I liked
though was the ability to define thresholds for different metrics
that will cause warning highlight colors to be applied
to the report to help you spot trouble quickly.
(When defining or adjusting your thresholds, remember to keep
in mind that vil, like NDepend, analyzes the IL rather than the actual
source, so metrics like Cyclomatic Complexity and Lines of Code may be
a bit inflated compared to what you may be used to).
So, I set my thresholds, added in my NAnt task, updated the ccnet.config and dashboard.config,
And…
The Final Result:

Pretty great huh? Can’t wait to put it in your continuous integration build right?!
Well, now that I’ve got you all worked up, let me give you the potentially bad news…
The version of vil that I downloaded last week from www.1bot.com
will only analyze assemblies or sets of assemblies with less than
100 types (classes, structs, interfaces, etc) before complaining that
it needs a license key. Now, I say this is potentially bad news because
the website documentation says that you can email the contact email
address to get a key (with no mention of this key costing anything),
but my attempts to get a response have so far been met with silence.
This may or may not be a problem for you depending on the size of your
codebase.
The other bit of bad news is that I can’t think of a simple way to
use vil to perform and report on more than 1 filtered query on my code.
This is because all of the information from multiple queries would
get merged into the main ccnet report xml, and the xsl would
apply templates to the whole set of generated
output. So, if I wanted, say, a list of the most
complex methods AND a list of the methods with the more than 5
locals in separate reports, this wouldn’t be easily doable. I suppose I
could somehow wrap the results in an outer grouping tag, and then
create modified xsl transforms to look at each specific group
separately, but this is more hacking than I want to do for now. Maybe
down the road. Or maybe a helpful reader will do it or figure out a
better way and save me the trouble (hint hint).
For now, I’m content to use vil to isolate a specific set of
metrics and highlight specific areas that might need refactoring,
while letting NDepend get the rest. I’m thinking I will actually use
the cyclomatic complexity query similar to the one above
that returns the top n methods in my main build report summary and
then use other code metrics tools for additional analysis.
So now I have a fork (NDepend) and a knife (vil). Will SourceMonitor be the spoon that completes my table setting? Find out in the next part of this series on Code Metrics and Code Metrics Tools….
Update: Good news! I got a
response about a key. The key gives you license to use the current
version of vil (or any updated version within the year) in an
unrestricted manner now and forever. No cost. No type limit. Slice and
dice away.