C# CODE ANALYSIS
.Net Code Analysis - NDepend Part-1
- How to use NDepend to analyze .Net Assemblies
- Dashboard report features.
- How Dependency Graph helps to improve code architecture and maintainability.
- 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
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:
- An HTML report is generated and opened in a different Tab on the default browser.
- 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.
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.
- 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.
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 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
- 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..!!
Stay tuned for more posts on Code Analysis with NDepend.
More on C#
Enforce code cleanup on build — .Net
Create code cleanup profiles and run on the build-in Visual Studio.