Recently I got a free professional license for NDepend and agreed to review it in my blog. In the beginning, I was a bit skeptical about this tool, but after trying it on my pet projects I've changed my opinion and now can recommend it to other developers.

First of all, NDepend is a static analysis tool for .NET that can be integrated into Visual Studio (as an extension) or used as part of your continuous delivery pipeline.

NDepend Dashboard First Result

There are a few main features available in NDepend:

    • Code visualization (dependency graph, dependency matrix, metrics view, etc.)
    • Code-base snapshot comparison
    • Quality Gates
    • LINQ based rules (CQLinq)
    • Build integration
    • Reporting

Code Visualization

I think it's the most powerful code visualization tool on the market. When your project grows above a certain complexity, it's really hard to keep track of all the dependencies in it. NDepend has a few tools to help you visualize your codebase.

Dependency Graph

With this tool, you can see dependencies between namespaces and .NET assemblies:NDepend Dependency GraphYou can as well visualize other dependencies in the project, please see the official documentation for more info.

Dependency Structure Matrix

Another way of representing dependencies is a dependency matrix.

NDepend Dependency Matrix

This will show you a bit more about the dependencies, for example, the number in a cell is the strength of the coupling represented by the cell.

Code-Base Snapshot Comparison

When you write code in a team, you want to keep code quality high and technical dept low (just like strong cohesion and low coupling). But you need a tool to measure these metrics over time. NDepend has a tool to keep track of your code quality on track.

Trend Charts On Dashboard

The idea is simple, you take a baseline snapshot, then you work on your code, take another snapshot and compare them. In this way, you can see the dynamic of changes. Is your code getting better or you introducing some technical dept? This tool will help you answer that question.

Quality Gates

A Quality Gate is a code quality goal you want to enforce before committing and\or releasing the code.

NDepend Quality Gates Summary

A lot of default Quality Gates are proposed by NDepend related to measures like code coverage, technical debt or amount of issues with particular severity.

CQLinq

With NDepend, you can write a LINQ query to analyze your code. There are already around 200 default rules available to you. You can modify them or create your own.

Here is an example of a custom CQLinq rule:

// <Name>UI layer shouldn't use directly DB types</Name>
warnif count > 0

// UI layer is made of types using a UI framework
let uiTypes = Application.Types.UsingAny(Assemblies.WithNameIn(
                "PresentationFramework", "System.Windows", "System.Windows.Forms", "System.Web"))

// You can easily customize this part to define what are DB types.
let dbTypes = ThirdParty.Assemblies.WithNameIn("System.Data", "EntityFramework", "NHibernate").ChildTypes()
              .Except(ThirdParty.Types.WithNameIn("DataSet", "DataTable", "DataRow"))

from uiType in uiTypes.UsingAny(dbTypes)
let dbTypesUsed = dbTypes.Intersect(uiType.TypesUsed)

let dbTypesAndMembersUsed = dbTypesUsed.Union(dbTypesUsed.ChildMembers().UsedBy(uiType))

// Per default this rule estimates a technical debt
// proportional to the coupling between the UI and DB types.
let couplingPerUIType = 2 +
   uiType.Methods.UsingAny(dbTypesUsed).Count() +
   dbTypesAndMembersUsed.Count()

select new { 
   uiType, 
   dbTypesAndMembersUsed,
   Debt = (4 * couplingPerUIType).ToMinutes().ToDebt(),
   Severity = Severity.Major
}

This rule first defines the UI layer and the DB framework layer. Second, it checks if any UI layer type is using directly any DB framework layer type.

Here is another example of the rule and the visualization of the result:

NDepend CQLEdition

Build Integration and Reporting

NDepend can analyze code via NDepend.Console.exe. In this way, you can easily integrate it with the existing continuous integration pipeline.

NDepend Report

Each time it analyzes a codebase, NDepend yields a report that will show you the status of your development. You can customize sections shown in the report and you can even provide your own XSL sheet for full customization.

Summary

NDepend is a powerful code analyzing tool with a reach set of features. I think it might be a bit overkilling for small codebases and small teams. Starting from medium size projects or a large codebase you will see the benefits of using this tool.

Of course, you need to invest in licenses, initial configuration and educate your team. That is why you should first calculate return on investment for your specific case and only then make a desition.

P.S. Most of the images for this article are from the official NDepend documentation