NDepend – The Ultimate Code Quality and Analytics Tool for .NET

NDepend - Code Quality and Analytics Tool for .NET

Last Updated on October 18, 2023 by Aram

NDepend is a comprehensive code quality and analytics tool for .NET applications, it relies on static analysis of the code to provide insightful details about a .NET project.

It offers a wide variety of analytical and productivity features including, dependencies analyzer, technical debt estimation, code quality analysis (quality gates), code query and rules using CQLinq and many other great features.

NDepend makes it easy to explore your project’s code with visualizations that can help in taking smart decisions regarding your project

It has even support for legacy code, to help you migrate with less pain and better quality.

It is known to the community as the Swiss Army Knife for .NET Developers.

Getting Started with NDepend

First you have to download NDepend from the official site and install it on your development machine.

Then open any of your projects in Visual Studio, note that NDepend can integrate with several versions of Visual Studio from 2010 until 2022

In this article I am using one of my Web API projects TasksApi using Visual Studio 2022,

if you want to follow me with the steps I am taking to run NDepend on this project, you can clone it from my GitHub account.

Once you clone it or you have your project ready, open the solution in Visual Studio.

From the Top Menu, Choose Extensions -> NDepend -> Attach new NDepend project to current VS solution

Then press ‘Analyze a single .NET assembly

Once the processing is done, you will redirected to a screen with different actions to be taken with NDepend:

View NDepend Dashboard

You will get a screen with tons of details about your project.

Analytics and metrics for your code you have never seen before without such a tool like NDepend

Lines of Code, Full analysis of types, assemblies, namespaces, methods and more

Quality Gates, Rules and Issues with breakdown

Technical Debt estimation to understand the cost and time implications in case you want to apply the suggested code fixes and refactoring

And many more

You can press on any of these metrics and you will get more details.

Quality Gates

These are code quality goals that must be met prior to releasing or committing to source control.

Quality gates are repesented with

Let’s try to press on the Fail number 1 in Quality Gates

You will get Critical rules Violated

Now if you double-click on this item, more details will pop-up in the Queries and Rules Edit panel on the right side

This quality gate specifies that you have violated a certain rules that have been identified as critical

NDepend can let you define the rules and which of them to be specified as critical

In this case, our code has violated one of the critical rules which says:

Avoid having different types with same name

Hovering on the message, will pop-up an extensive tooltip providing more information about the violated rule:

Now let’s hover on the 1 issue, the second tooltip explains more about the exact issue in specific

We are using Task as an Entity and it has another use.

Now double-click on 1 issue, you will get a new panel with details about the usages of the Task Type and where is it duplicated

Now let’s hover on 2 types, you will get a tooltip with the 2 usages for the Task type

Obviously, Task is already a defined class in System.Threading.Tasks namespace

Again, this doesn’t mean that the code won’t compile or is buggy, but this quality gate tells us how to make your code less prone to confusion and specific to your project needs. It can also help in avoiding naming collision.

Let’s explore another section of the NDepend dashboard,

see the Issues panel:

We can see that there are 4 high Issues.

Click on it to expand and see what are the issues

Note that we have already checked the 2nd issue which is related to types with the same name.

This section is grouping the issues per rules.

And you can read more about the issues and their potential solutions in the same way we have checked earlier in this article.

Of course you should get to back to the official docs to learn more about the static code analysis and quality gates.

Technical Debt Estimation

This is another major feature provided by NDepend.

Technical Debt is a critical topic and not new in Software Engineering.

It is represented by the cost incurred if the specified issues are not resolved. These issues are mainly due to applying quick solutions and developing features that are still incomplete and the requirements are undergoing multiple changes, and many other concerns.

All these can cause the currently developed solutions to require a rework (revamp) in the future and the issues can build up like with a compounding interest that would keep the technical debt growing and costing more.

NDpened has the feature to smartly detect the technical debt, estimate it and provide complete analysis for all the issues using the embedded Code Query LINQ formulas.

This was only the tip of the iceberg on technical debt and its estimation.

This link covers the complete explanation of this widely important topic and provide examples.

Code Quality and Rules Language CQLinq

All the quality metrics and rules provided by NDepend use a new feature called the Code Query LINQ.

CQLinq provides a set of LINQ commands and extensions that allow querying your code base.

NDepend comes with massive number of predefined rules and metrics, each rule and metric is a code query.

So you can either use a predefined rule or you can create your own.

Let’s take a look at the earlier example of Failed Quality Gate for Violating Critical Rules, how is the Code Quality LINQ for this rule is written:

See? It is the same LINQ structure but here the code is about querying the code itself.

Any code quality rule or metric CQLinq is accessible from the Queries and Rules Edit panel.

You can learn more about CQLinq here.

Dependency Graph

NDepend is a comprehensive tool that is packed with great features to boost your productivity and enhance your project and code quality.

This is another great feature of NDpeend

You can check the dependency graph for your project by going to Extensions -> NDepend -> Dependency Graph

Or just use the keyboard shortcut ALT+G

Below is the dependencies graph for TasksApi

Going inside the Controllers Namespace, we can see the actual dependencies required by the Controller classes:

Code Metrics View

We can choose to view the code metrics through graphical representations.

NDepend gives you the ability to detect areas in your project where there are lots of lines of code in a single file,

You can also tell how complex is your application with the cyclomatic complexity, which is a metric that can tell how many independent paths or decisions are done in your code to implement some functionality.

The higher the number the more complex is your code.

With NDepend Code Metrics View you can explore the different metrics with graphical representations and heat maps.

Below is a heat map for the lines of code, where the yellow shapes represent the types with higher density in lines of code

And the below is when selecting one of the heat maps, it gives more insight about the lines of code.

As mentioend earlier, you can choose different metric and that will be reflected on the heat map view

Here we have chosen the Cyclomatic Complexity to be displayed per size:

Dependency Matrix

It is similar to the dependency graph representation but has some great use cases, the dependency matrix can provide some useful insights on the code by determining the Code Structure Pattern

You can detect stuff like:

layered structure or if you have cyclic dependencies it will display in red square.

It can also find indirect dependencies

there are a lot more use cases and patterns that can be detected using the dependency matrix

You can read more in the official docs

NDepend Report

NDepend will put every single detail of its analysis into an interactive HTML page.

To generate the NDepend Report, Goto Extensions ->NDepend -> More -> Run Analysis and build report

Once the analysis is complete, your browser will display a comprehensive report as the below:

The report is heavily loaded with data, and there are different clickable links in many sections that would expand and show you even more details on specific metrics or findings.

See the below link after clicking TokenHelper.cs

Selecting the first rule show you another page with many details about the rule and it will group the different types as the below:

More Features

The above were just some of the main features of NDepend.

There are still a lot more, including:

  • Code Diff Feature: Compare the analytics data between older (baseline) and newer reports and see how things got changed.
  • CI/CD integration, so that your code will be checked for quality during development and in your DevOps pipeline
  • VisualNDepend.exe, a standalone executable that allows full usage of NDepend features outside of Visual Studio and it can support integration with Visual Studio Code and Rider

You can see and read about all the features of the great NDepend here.

Summary

In this article we have explored a hidden gemstone in the world of .NET.

NDepend is your ultimate code quality and analysis tool in Visual Studio that you would ever need.

I would say it is: The expansion pack of visual studio.

If you or your team are meticulous about quality of your code and product, then NDepend is a must-have tool to build a robust .NET solution.

Check it out yourself, you can test it for 14 days as a free trial.

Ready to build quality .NET application? Get your license today.

Bonus

Exploring this sophisticated and brilliant tool should be accompanied with a classical masterpiece.

Presenting to you Mozart Clarinet Concerto in A major K 622

Enjoy it and happy quality coding and learning with NDepend.

One Comment on “NDepend – The Ultimate Code Quality and Analytics Tool for .NET”

  1. Sad thing is, it doesn’t have CLI or VS Code integration. VS is slowly but surely seems will be replaced with code for webapi/avaloniaUI development

Leave a Reply