Review: NDepend v4

I’ve previously written about using NDepend, by Patrick Smacchia, to analyze code compiled for .NET and generate useful metrics and diagrams. That was last about version 2, as part of my .NET Tooling series. I am now on version 4, and thought it worth exploring some of the changes to the tool since then. I won’t go into the whats, whys, or hows about NDepend, which are well-covered in my previous post, but will focus on changes in the new version.

CQL and CQLinq

Past versions of NDepend have had a built-in language for generating code metrics, called Code Query Language (CQL). A sample for generating warnings on unsealed classes with no subclasses looks like:

// <Name>Class with no descendant should be sealed if possible</Name>
  IsClass AND 
  NbChildren ==0 AND 
  !IsSealed AND 
  ORDER BY NbLinesOfCode

Recent versions of .NET languages C# and VB have supported the use of Language Integrated Query (Linq) for querying collections and retrieving matches using a syntax similar to Structured Query Language (SQL). A CQLinq analog to the above CQL sample follows:

// <Name>Class with no descendant should be sealed if possible</Name>
warnif count > 0 from t in JustMyCode.Types where 
  t.IsClass && 
  t.NbChildren ==0 && 
 !t.IsSealed && 
  orderby t.NbLinesOfCode descending
select new { t, t.NbLinesOfCode }

It seems that one option is not better than the other, yet the CQLinq format closer resembles what C# developers will be used to writing. That alone may make using the new format worthwhile. Both versions – CQL and CQLinq – are being supported, though the CQLinq may be a natural selection for those used to the Linq syntax.

The CQLinq syntax is well-documented on the NDepend website. There is also some documentation on converting CQL rules into CQLinq equivalents.

Other Changes

CQLinq is possibly the most notable change in this version of NDepend. There are other changes worth pointing out, however, which further improve the utility of the tool. One of these is integration into other tools.

NDepend supports integration with Visual Studio 2008, 2010, and 2012. By installing the integration extension, an NDepend menu appears in the selected Visual Studio versions. This works even if multiple supported versions of Visual Studio are installed – NDepend can integrate with any and all of the three. The menu provides options to create an NDepend project, analyze selected solutions, projects, or assemblies, or to start the separate NDepend application.

An additional integration is with Reflector, the widely-used assembly decompilation tool. With the integration enabled, right-clicking an assembly in Reflector shows a context menu, which includes an NDepend submenu. This submenu provides options for analyzing the assembly in NDepend, including the dependency matrix and the metric view. This is a timesaver for peeking into third-party assemblies, or even self-generated ones.

Another notable change is an NDepend API, which can be utilized when writing custom rules. With this capability, it is possible to write power-tools for NDepend to add rules for running analyses. Installed power tools can be accessed from NDepend’s File->Start Power Tools menu option. This brings up a command-line window with a prompt for running tools of interest, as seen below.

A console window lists NDepend's power toolsThe console window providing access to NDepend’s power tools

NDepend’s report-generation capability has been updated. In version 2, reports were static HTML documents with some images and styling. In version 4, NDepend’s HTML-based reports have some interactivity to show and hide items of interest, as well as some refreshed styling.

Final Thoughts

The user interface doesn’t seem to have changed much. It is still a complex experience as implied in the previous review, and requires some learning before it becomes familiar. It is also best used on larger screens where there is more space to work with – the different panels quickly crowd smaller displays.

Once understood, the UI makes analyzing assemblies, running rules, and generating reports easy. That was the case previously, it still is now. The new CQLinq syntax for writing analysis rules, along with the API, make code analysis a (somewhat) easier task than it was previously. These all make the new version of NDepend worth taking a look.

Further Reading

There are numerous articles online on the use of NDepend’s features. Some interesting ones are linked here. In particular, go to Patrick’s website and review the posts archive for the second half of the year 2012; there are a number of useful posts about putting NDepend to work.

Related Posts