NDepend 2.2

A few months ago, I wrote about Visual Studio 2005 Professional Edition and FxCop Integration and also the upcoming Visual Studio Code Metrics that will be available in the Visual Studio “Orcas” release. These are both great tools, but one is really only available if you purchase one of the Visual Studio 2005 Team Edition versions (at least, available without some extra work) and one isn’t available yet except through a public beta.

However, if you want the type of code analysis that you can get with the upcoming Visual Studio Code Metrics and don’t mind not having it integrated with Visual Studio, you should definitely check out NDepend.

NDepend is an excellent tool that is designed to provide a very deep analysis of your compiled code to help you understand and control your development effort by managing both the quality and the complexity of your code. NDepend works in a very similar way as the Microsoft FxCop tool, by analyzing your compiled code and calculating different metrics and statistics on that code.

Before you start thinking that NDepend is just an FxCop clone, it’s not. In fact, NDepend and FxCop are very good compliments to one another. FxCop analyzes your code against a well defined set of “best practices” covering everything from naming conventions to proper pattern implementation. Think of FxCop as an automated code review. FxCop rules are generally generic and apply to any .NET application.

NDepend analyzes your code against more than 60 different metrics and can be customized to a particular application. These metrics measure things like:

  • Lines of Code, Comments and the percentage of comments
  • Number of IL instructions
  • Number of Assemblies, Namespaces, Types, Methods, Fields
  • Coupling (both Afferent and Efferent), Cohesion and Cyclomatic Complexity
  • Instability
  • Abstractness
  • Depth of Inheritance

If you don’t know what some of these are or why they are important to you, NDepend has an excellent definitions page that lists all of the metrics covered, provides a definition of what it is and why it is important. Scott Hanselman has an excellent post describing how one of the NDepend metrics can be used to improve the quality of your code by examining the dependencies between assemblies.

Taking things a step further, NDepend also provides it’s own SQL-like query language called Code Query Language (CQL). CQL is a language which allows you to write queries against the code structure of any .NET application no matter which language was used to create it. This is an immensely powerful capability as it allows you to write your own custom metrics. For example, if you wanted to find all of the methods that have more than 200 Intermediate Language (IL) instructions ordered from largest to smallest, you would run a CQL query like this:

SELECT METHODS WHERE NbILInstructions > 200 ORDER BY NbILInstructions DESC

This type of analysis is nearly impossible to do by hand.

In order to use NDepend, there are several tools available to you. The first one is the NDepend Project Editor. This is the tool that allows you to create your NDepend project, which is where you specify the assemblies to analyze and what metrics to run.

The actual work is done by a console application. At this point, you might be wondering how you integrate this into your build process. NDepend comes with tasks for NAnt and MSBuild that allow you easily run the console application and give it the path of your project file. It even comes with a default XSL stylesheet for integration with CruiseControl.NET.

If you aren’t running it as part of your automated build process, you can still run the analysis and generate an HTML report or view the results in VisualNDepend.

If you have access to a large-format printer or plotter, you can even generate posters (up to 10000 x 10000 pixel PNG files).

NDepend comes in two different editions:

  • Trial/Open Source/Academic
  • Professional

The Trial edition can be used on commercial projects within a “reasonable” amount of time for evaluation (around 2 weeks is more than enough time), in an academic setting for educational and research purposes, and for open-source projects.

There aren’t many differences between the two editions and a full comparison is available, but the extra features you get with the Professional edition are:

  • Store custom CQL queries and constraints
  • Insert CQL queries and constraints in source code using the CQLConstraint attribute
  • CQL constraint violations are highlighted in the report
  • Create custom documentation from a CQL query or constraint in VisualNDepend
  • Build comparison
  • Indirect dependencies mode in the VisualNDepend Dependencies view
  • Open source code definitions in Visual Studio from VisualNDepend

If all of this seems overwhelming, don’t worry. The NDepend web site has some excellent screencasts to help you get started.

Tools like NDepend can really give you a lot of insight on how your applications fit together. They can be difficult to learn at first, but once you do they are well worth the effort. In order to make the best use of this information, you really need to integrate it into your build process and your software development process. We are running this on the Subtext project and I am actually in the process of integrating the results with our CruiseControl.NET dashboard.