Introduction to NDepend

12 mei 2021 om 10:00 by ParTech Media - Post a comment

A developer must take efforts to follow the norms of best practices and the coding guidelines as per the industrial or organizational standards. The simplest way to make sure that the code follows all the guidelines according to a standard is by manually analyzing your code or making use of any static code analyzer like FxCop, StyleCop.

However, you are required to automate all the tasks as it saves time and money where there are repetitive tasks, thus improving efficiency. Static code analyzers play a major role here. NDepend is one such widely used static analyzer if you have your codebase in C#. NDepend highlights your code mistakes and can even provide a rough estimate of the debt in terms of the duration required for the development.

Table of contents

  • What is static code analysis?
  • NDepend in .Net
  • How to install NDepend?
  • Features of NDepend
  • Dependency matrix
  • Dashboard
  • Conclusion

What is static code analysis?

Static code analysis involves performing analysis of computer software without executing the actual program on a certain piece of the source code. In some cases, the intermediate compiled code or a form of object code is analyzed. Such a type of analysis is called static code analysis.

The analysis on basis of characteristics of individual statements and declarations to analyzing the entire source code determines the sophistication of the static programs.

NDepend is one such static code analysis tool that can be used for CI integration for DevOps efficiency and daily development.

NDepend in .Net

NDepend in .Net is a static analysis tool that can be run on Visual Studio, or on your build server or even on a standalone application. Starting from 2010, NDepend is a Visual Studio extension supported by all the versions of Visual Studio. Besides, NDepend supports major .Net versions such as .Net Core 3.1, ASP.Net Core 3.1, .Net Framework 3.x, etc.

Insights such as code quality analysis via LOC (Line Of Code) and methods complexity is available in NDepend. NDepend has its language similar to LINQ. Here, existing queries can be edited and new ones can be created. A large number of code metrics are supported in NDepend. The dependencies are visualized using a dependency matrix and directed graphs. Code-based snapshot comparisons are performed in NDepend along with validation of the quality and architectural rules.

How to install NDepend?

Since NDepend is a commercial tool, therefore, a license needs to be procured. There can be two types of licenses: Developer Seat ($477 approximately per seat) and Build Machine Seat ($955 approximately per seat).

Following are the steps to install NDepend:

Step 1: Once the license is obtained, the NDepend_2021.1.0 zip package can be downloaded. This is the latest version of NDepend which was available when the article was written.

Step 2: Extract the zip file components into a local folder.

The major components of the zip file are:

  • NDepend.Console: This is the command line program where you can execute NDepend analysis. This is usually used on Continuous Integration (CI) build servers.
  • NDepend.PowerTools: This tool can be used to create your analysis tool based on NDepend API or make changes to the existing open-source power tools.
  • NDepend.VisualStudioExtension.Installer: This is used to make NDepend as an extension in Visual Studio.
  • VisualNDepend: It provides you an independent visual environment where you can manage your NDepend tasks.

Features of NDepend

  • It provides you with dependency visualization which can be done through dependency matrix and graphs.
  • You can analyze and generate different software quality metrics. NDepend supports 82 different quality metrics.
  • NDepend provides LINQ queries that support declarative rules. These queries are known as CQLinq queries. NDepend provides a large number of CQlinq rules which are predefined.
  • NDepend provides integration support for Cruise Control.Net, SonarCube, and City. Code rules can be configured in a way that they are checked automatically during Continuous Integration (CI) or in Visual Studio.
  • With NDepend API you can easily analyze one too many .NET assemblies and create NDepend reports.
  • NDepend makes it easier to programmatically generate on the fly and execute CQLinq rules or queries.

Dependency matrix

In the dependency matrix that the NDepend generates, you can see the dependencies between any of the two code elements and at any level. The goal of the dependency matrix is to visualize and check the coupling between the components. The dependency between the components can be based on various parameters like namespace, member, direct and indirect depth of use, types, fields, and methods. When any of the cells are clicked, detailed information about the dependency between the two code elements represented by the cell is shown.

With the setting weights on cells = Direct # Method, this dependency matrix also gives a very good view of coupling in terms of method calls.

Dashboard

After the NDepend has finished analyzing the code, there’s a report with the summary of analysis in the form of a dashboard. The dashboard provides a high-level analysis of the source code. Let us look into the important insights captured from the dashboard:

#lines of code: This metric provides the number of lines of code analyzed.

Method complexity: The cyclometric complexity gives the number of paths that are independent through the source code of a program. A value higher than 15 signifies that the methods are tough to comprehend and maintain.

Code coverage by tests: In Visual Studio Premium, you get the facility of advanced code coverage. The code coverage result provides the percentage of LOC covered and the number of LOC not covered.

Code rules: The code rules section provides the set of critical and non-critical rules that are violated by the source code.

Conclusion

NDepend is one of the most popular commercial static analyzers which are enterprise-grade. The extensive level of reports provided by the NDepend analyzer is superior to the other analysis tools like the Visual Studio Code Analysis, FxCop, and StyleCop Analyzer. It offers great ROI for the money spent by providing customers with more than what they expect of it. NDepend makes the life of a developer easy by simplifying the DevOps lifecycle as you can integrate NDepend in the build process.