NDepend is a code analysis tool that lets you analyze your code by writing SQL-like queries (called CQL), such as:
SELECT TOP 50 TYPES ORDER BY TypeRank DESC
SELECT METHODS WHERE NbParameters > 5
NDepend gives you the ability to analyze your code in just about any way you can think of. And of course you can get reports on it, add it to your automated build process, etc. You can even embed your CQL code inside your "real" code :)
I may as well move onto the bad news - it is overwhelming at first. The GUI definitely doesn't help:
I had to squish it pretty good to give you any idea of all the things it has. Let's look at some of the individual windows now:
The Class Browser Window:
Which turns into the CQL Query Results Window when there are results to view:
The CQL Queries Window:
And many more. Did I mention there is a lot here? Let's dive in a bit...
If you click on the "Source Code Quality constraints" CQL group and then on the "Methods too big (NbLinesOf Code)" query, NDepend runs that query. The Class Browser window becomes the CQL Query Results window and you see something like this:
If you were to double click (to edit) the query, it looks something like this:
In the query above, I'm looking for all the methods that have more than 30 lines of code that are NOT in the DAL or my underlying service class. Should I consider 30 lines too many? Form your own opinion. But the point is that NDepend lets you create your own "rules" to analyze your code with. This is just an example :)
NDepend comes with a LOT of pre-defined queries to help you get started and to analyze your code. Let's dive into one dealing with Cyclomatic Complexity.
The CQL Query:
Hovering over the one method it found reveals an "Info" window:
and it also updates the "graphic metrics" view, highlighting in RED where that method lives:
There is so much more to tell, but I must admit I'm only beginning to use this tool. It has very quickly become something I consider to be indispensible in my development practice.
Let's look at some more sample queries:
SELECT TYPES FROM ASSEMBLIES "AssemblyName" WHERE IsDirectlyUsing "DAL.Factories"
For example, you might want to make sure that your GUI classes are not directly speaking to your Data Access Layer.
SELECT FIELDS WHERE CouldBePrivate
Use this to see fields that have too much visibility. This is determined by the fact that they are never accessed outside of their own class.
SELECT TYPES WHERE DepthOfInheritance >= 6
Inheritance is great, but... You don't want to get too "deep."
What NDepend really needs is a set of forums. So that users like us could share our queries :) Hmmmmm...
Here are some links about NDepend:
A review of NDepend at dotNetSlackers
Integrating NDepend in to NAnt / CC.NET
A printable poster from Scott Hanselman
A Hanselminutes Podcast about NDepend
I should mention that the NDepend site itself has some GREAT documention:
Articles on Code Metrics and Code Analysis in general:
In Pursuit of code quality: Refactoring with code metrics
In Pursuit of code quality: Code quality for software architects
How Useful Are Code Metrics?
Code Metrics: Driving Up Quality with Automated Review
Measuring Complexity and Maintainability of Managed Code
BONUS: Patrick Smacchia (the developer of NDepend) has graciously offered a free copy of NDepend for me to give away at Code Camp this weekend!