.Net Code Analysis - NDepend Part-1
Tags: csharp, dotnet, codequality, todayilearned
Published: 2021-05-12 14:19:32

### Learn to analyze .Net applications using NDepend.


## Learning Objectives

* How to use NDepend to analyze .Net Assemblies

* Dashboard report features.

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

## Prerequisites

* First, Install the latest Visual Studio [here](https://visualstudio.microsoft.com/downloads/).

* Download NDepend Trail Edition [here](https://www.ndepend.com/download)

* Unzip the download and install the VS extension

Official Documentation f[or more info](https://www.ndepend.com/docs/getting-started-with-ndepend)

## 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.

![](https://cdn-images-1.medium.com/max/2000/1*pylynYPE6-zOJC7HX7N97w.png)

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

![](https://cdn-images-1.medium.com/max/2000/1*hgqki5Tci7bs7P-MhlOgHw.png)
> # **“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](https://cdn-images-1.medium.com/max/2000/1*1z0MynaR8lS_Fu63dJG4cA.png)

## Dashboard Features

### Debt v/s Rating

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

![](https://cdn-images-1.medium.com/max/2000/1*FDoQTe9J6pAPmDLmhhLPgw.png)

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.

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

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

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

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

* 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.

![](https://cdn-images-1.medium.com/max/2000/1*6LYax4hKFenpEL29Jepg2A.png)

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

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

* Using debt information for estimation.

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

## Improve Code Architecture & Maintainability

[Dependency Graph](https://www.ndepend.com/docs/visual-studio-dependency-graph) helps understanding code by visualizing its architecture.

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

* 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

* assemblies

* namespaces

* types or members

![](https://cdn-images-1.medium.com/max/2000/1*n7Vyl3BjnDBwSPpQaGQsUQ.png)

### 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.

![](https://cdn-images-1.medium.com/max/2000/1*2pVdglhoYfvmyHNZdlDWiA.png)

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..!!

![](https://cdn-images-1.medium.com/max/2000/1*ObZu588zvWcDkHKjB4X_9Q.png)

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section. Follow me on [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh-532284a2/), [Instagram](https://www.instagram.com/sukhpindersukh/) [Facebook](https://www.facebook.com/sukhpinder.singh.52/) [Twitter](https://twitter.com/sukhsukhpinder)

Stay tuned for more posts on Code Analysis with NDepend.


<a href="https://www.buymeacoffee.com/sukhpindersingh" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>