Joydip Kanjilal
Contributor

Measure the quality of your .Net code with NDepend

how-to
Mar 25, 20196 mins
App TestingDevelopment ToolsMicrosoft .NET

Take advantage of the powerful static code analysis tool to identify and understand issues in your .Net application’s code

1 measure roi
Credit: Thinkstock

Assessing the quality of an application’s code is often a subjective process. This is why we turn to code metrics — quantitative measurements that provide valuable insights into our application’s code. Developers can take advantage of code metrics to understand the quality of the code, get an idea of the potential problems, and identify which types and methods need to be refactored to improve the quality.

Static code analysis tools are used to measure the quality of code in an application without having to execute the application. There are many static code analysis tools available for .Net. These include FxCop, StyleCop, ReSharper, CodeIt.Right, NDepend, etc. This article presents a discussion of how we can use NDepend to visualize code quality and adopt measures to improve it.

What is NDepend and why use it?

NDepend is a static code analyzer that integrates nicely with Visual Studio and provides valuable metrics for determining code quality. NDepend displays these metrics in the form of lists, matrixes, graphs, tree maps, and charts. Along with these metrics, NDepend can analyze your code and report violations against a large database of rules.

NDepend saves the results of each analysis, so you can compare the results after you’ve improved upon the code coverage or code quality. It can be run both as a standalone application and as an extension in Visual Studio. And you can take advantage of the NDepend Azure DevOps extension to integrate NDepend with your projects in Azure.

The following are some of the striking features of NDepend:

  • Large set of code rules to check for compliance
  • Many charts and diagrams for viewing code metrics
  • Integrates nicely with Visual Studio 2010, 2012, 2013, 2015, and 2017
  • Enables you to explore the dependencies between modules
  • Provides CQLinq (Code Query over LINQ) to customize analysis
  • Measures code coverage
  • Identifies difficult-to-maintain code and estimates technical debt 
  • Creates trend charts

Getting started with NDepend

To get started using NDepend, you’ll need to download and install a copy of it. You can download a trial copy of NDepend from here. It will be packaged as a .zip file. 

Once you have unzipped the NDepend installer .zip file, you will find the following files inside:

  1. NDepend.Console — used for integration with the build process
  2. NDepend.PowerTools — a collection of open-source static analyzers
  3. NDepend.VisualStudioExtension.Installer — installs the Visual Studio Extension for NDepend
  4. VisualNDepend — a GUI client for NDepend

You can install the Visual Studio extension of NDepend in one of the following two ways:

  1. Run NDepend.VisualStudioExtension.Installer
  2. Run VisualNDepend and select “Install Visual Studio Extension”

Once Visual Studio Extension for NDepend is installed, you can leverage the metrics, rules, reports, graphs, analyzer results, and tools from within the Visual Studio IDE. You can also take advantage of Visual NDepend to analyze your solutions and projects without having to of launch Visual Studio.

The Visual Studio Extension for NDepend is available for Visual Studio 2010 through Visual Studio 2017. Note that I am using it with Visual Studio 2017.

Analyzing source code with NDepend

To start analyzing your source code, you can click on “Analyze VS solutions and VS projects.” The other options available are “Analyze .Net assemblies in folder,” “Analyze a set of .Net assemblies,” and “Compare 2 versions of a code base.” That last option lets you compare two builds — a nice feature!

For the sake of simplicity, I will be using the source code from one of my recent articles here (“How to use Lamar in ASP.Net Core”). Once you click on “Analyze VS solutions and VS projects,” a new window will be displayed as shown in Figure 1 below.

ndepend figure 1 IDG

Figure 1: Select one or more Visual Studio solutions or projects to be analyzed. 

You can now click on “Browse” and specify the solution file of the project you would like NDepend to analyze.

ndepend figure 2 IDG

Figure 2: Selecting the VS solution to analyze. 

Lastly, click on “Analyze a single .Net assembly” to start the analysis as shown in Figure 3 below.

ndepend figure 3 IDG

Figure 3: Analyzing a .Net assembly. 

This will run a code analysis on the selected project. When the analysis is complete, NDepend will display a window with the following options:

  • View NDepend Dashboard
  • Show NDepend Interactive Graph
  • Browser NDepend Code Rules
  • Close the dialog

Let’s choose to view the NDepend dashboard. Here’s what the output looks like: 

ndepend figure 4 IDG

Figure 4: The NDepend dashboard. 

NDepend shows you the lines of code, information on types, debt, comments in the source code, coverage information, method complexity, quality gates and the rules that have been violated and other issues.

NDepend features

Dependency graph and dependency matrix

NDepend also shows you a dependency graph and dependency matrix of your code. While the former provides a graphical view of the dependencies in your project, the latter provides a tabular view of your code organized by dependencies on namespaces and types.

Trend charts

Trend charts are available in the dashboard. These charts provide a preview on how the quality of the project’s source code has been changing over time. You can also create your own custom trend charts.

ndepend figure 5 IDG

Figure 5: Trend charts in NDepend. 

Cyclomatic complexity

Cyclomatic complexity is a quantitative measure of the linearly independent paths in source code that can help you understand the complexity of your program and improve code coverage. You can take advantage of NDepend to analyze the cyclomatic complexity in your application’s code. You can read more about cyclomatic complexity in this article.

ndepend figure 6 IDG

Figure 6: Measuring cyclomatic complexity in NDepend. 

Querying using CQLinq

CQLinq is one of the most striking features of NDepend. CQLinq enables you to query .Net code using LINQ. You can take advantage of CQLinq to query code debt, issues, rules, and quality gates.

ndepend figure 7 IDG

Figure 7: NDepend’s CQLinq in action!

Let’s look at an example. The following CQLinq query displays the method names and cyclomatic complexity of non-abstract methods that have a cyclomatic complexity greater than 20, ordered by descending order of the value of cyclomatic complexity.

from m in Application.Methods
where m.CyclomaticComplexity >= 20 && !m.IsAbstract
orderby m.CyclomaticComplexity descending
select new { m, m.CyclomaticComplexity }

Here is another example of a CQLinq query. This one when executed will display the names of the methods that have more than 100 lines of code.

from m in Methods where m.NbLinesOfCode > 100 select m

To learn more about CQLinq, you can refer to the NDepend documentation here

NDepend is a static code analysis tool that can give you valuable insights into your application’s code. You can run NDepend as a standalone application or integrated in Visual Studio. Although NDepend is not free, it is reasonably priced considering its powerful capabilities, and it is available for all recent versions of Visual Studio.

If you want a great resource for learning NDepend, I highly recommend the Pluralsight course “Practical NDepend” by Eric Dietrich. 

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author