.Net Code Analysis – NDepend Part-1

C# CODE ANALYSIS

Learn to analyze .Net applications using NDepend.

Photo by cottonbro from Pexels


Learning Objectives

  1. How to use NDepend to analyze .Net Assemblies

  2. Dashboard report features.

  3. How Dependency Graph helps to improve code architecture and maintainability.

Prerequisites

  1. First, Install the latest Visual Studio here.

  2. Download NDepend Trail Edition here

  3. Unzip the download and install the VS extension

Official Documentation for more info

Getting Started

Post-installation, to attach the code analyzer to any .Net application is just a “2-step process”.

Step 1: Open any .Net project in Visual Studio and click on the below icon and start analyzing the code assemblies.

Step 2: Click “Analyze a .Net Assembly.”

“It’s that simple to attach the code analyzer”

Dashboard View Features

The favorite part is the dashboard report. It provides lots of information in a single view that helps a lot.

There are two choices to view the report as given below:

  1. An HTML report is generated and opened in a different Tab on the default browser.

  2. A new dashboard tab will open in Visual Studio itself.

I personally prefer option 2 because it redirects to me the exact line of code or file where rules are violated.

Option 2 Dashboard View


Dashboard Features

Debt v/s Rating

Let us understand how the .Net Assembly is rated “A.”

The rating depends upon the “Debt Percentage,” and Debt is calculated based upon calculated technical debt compared to the number of code lines required to re-write the code from scratch.

  1. If Debt lies between 0 to 5%, it’s Rated “A.”

  2. If Debt lies between 5 to 10%, it’s Rated “B.”

  3. If Debt lies between 10 to 20%, it’s Rated “C.”

  4. If Debt lies between 20 to 50%, its Rated “D.”

  5. If Debt is more than 50%, it’s Rated “E.

View Debt per file

Click on the rating alphabet, and the tool shows the debt per file.

The most valuable part, one can choose to export the above categorization into various file formats like HTML, Excel(a personal favorite), etc.

The ultimate use case is

  1. To dump the breakdown into an excel sheet and export it to JIRA to create tasks or user stories.

  2. Using debt information for estimation.

  3. Using the debt grade of each file as a priority.

Improve Code Architecture & Maintainability

Dependency Graph helps understanding code by visualizing its architecture.

  1. It helps redesign the regions where code health & structuring is poor.

  2. It also helps in protecting the codebase against future shortcomings with improved and optimized architecture.

What is a Dependency Graph?

Dependency Graph shows errors, potential obstacles, and code smells. It helps to expose bugs that the codebase may be trying to protect.

A dependency graph consists of the following components

  1. assemblies

  2. namespaces

  3. types or members

How to view the dependency graph with NDepend?

Users can export/view anything to a dependency graph. The best way to view is just right-clicking on any “.cs” file and choose “Show on Dependency Graph,” as shown below.

The particular file will be highlighted in the graph as shown below. Personally, it helps me visualize how deeply the class file is linked.

Dependency Graph of .Net Core application with more than 10k+ lines of code. I find the tool really powerful to graphically represent such a huge codebase within 2–3 seconds. Well done, NDepend..!!

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section. Follow me on LinkedIn, Instagram Facebook Twitter

Stay tuned for more posts on Code Analysis with NDepend.

More on C#

View at Medium.comView at Medium.comView at Medium.com

#CodeQuality #Ndepend #CodeAnalysis #DotnetCore #Dotnet

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w