C# CODE ANALYSIS

.Net Code Analysis - NDepend Part-1

Learn to analyze .Net applications using NDepend.

Sukhpinder Singh
May 12 · 4 min read
Photo by cottonbro from Pexels

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.
  • Download NDepend Trail Edition here
  • 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

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.

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

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.

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

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

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

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Sukhpinder Singh

Written by

Programmer by heart | C# | Python | .Net Core | Xamarin | Angular | AWS | Remember to 👏👏👏👏 Buy me a coffee https://www.buymeacoffee.com/sukhpindersingh

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Sukhpinder Singh

Written by

Programmer by heart | C# | Python | .Net Core | Xamarin | Angular | AWS | Remember to 👏👏👏👏 Buy me a coffee https://www.buymeacoffee.com/sukhpindersingh

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store