Writing beautiful, optimized and better .NET code with NDepends static analysis

Introduction

Over the years I’ve been in various discussions regarding benefits of clean architecture, best practices, techniques such as code reviews, unit tests etc. and I think on some degree most of us are aligned on the reasons behind it.

Having a clean architecture or code-base not only makes your development team happier, but it has far reaching impact on business itself.

In this post, we will learn about NDepend, which is described on their website as following

The “Swiss Army Knife” for .NET Developers, Architects and Teams

https://www.ndepend.com/

In this post, we will take first steps and will see how to install and setup the NDepend in visual studio project and get started with improving code almost immediately.

Setting the Scene

You can setup NDepend on your machine easily by following steps:

  • Download the zip file.
  • Run NDepend visual studio extension installer
  • Use NDepend.

Following 3-minutes video from NDepends’ website show these steps:

So, I followed the steps as shown in the video and get NDepend setup in minutes.

Analyze .NET Code Base with NDepend

Lets see the code base, I will be using for the testing purposes. I’ve a very simple .NET project with following structure:

As you can see it is a typical .NET Core Web API project (demoApp.Web) and it references two standard libraries project for domain model (demoApp.Core) and database repository (demoApp.Data).

You can download the source code form this repository or use your own codebase and principles will be the same.

Now I am using this very simple codebase to keep the focus on NDepend in this introductory post and same time I am very well aware of this application code structure and component dependencies.

But as you may be already thinking, what about the complex code bases? and what if the developer is new to the project? This is where NDepends shine and once you start using NDepend, it will make it much easier for you to navigate the code base, visualize dependency graphs and off course help you find code issues, code smell and other architectural concerns in an easy and rapid manner.

You can visit official website for more information.

Back to installation step, once NDepend is installed, you will find the options as shown below:

Click Attach New NDepend project to Current VS Solution and with this one click, VS solution will be configured with NDepend, and on next screen, you can start Analysis with click of a button as shown below:

Analysis Results

Once NDepend ran through the code assemblies, it will generate an HTML report and also provide you some options to user for the next steps:

As you can see possible actions and corresponding description. Following picture display NDepend Dashboard:

and you can see this view gives us a good overview of code analysis and we can now drill deeper for the issues/concerns and see more details and take necessary action to improve the code base.

Now, the information and options from NDepend may seem overwhelming and that is ok, take your time to check each options. There are short videos for each feature form NDepend on their website and you can gradually learn these options and apply to your code base as you progress.

Context is the King

Context is the king in land of architecture. While NDepend can help you with analyzing your code base and providing easy navigation to different concerns and assist you to improve your code base but its you, as a developer to evaluate those recommendations and either accept the recommendations or not:

NDepends have many built-in code quality check and rules which looks for common issues and suggest best practices.

You can also introduce new rules as per your project requirements to configure NDepend to meet your specific needs and evolve your application architecture while following your internal best practices and policies.

Here is an example from analysis where NDepend identified 2 issues in dashboard with high severity in the code base:

We can drill down to the issues and see which rules are violated:

As you can see, along with rule description, NDepend is providing suggestion to fix the issue and with a simple click, it will take us to corresponding code area to fix it.

and here is the code which is not following the rules:

Now, developer can review the code and fix the issue following the suggestions suggested by NDepend.

Dependency Graphs

Dependency Graphs is another useful feature from NDepend which help you visualize the solution’s dependency graph. It becomes extremely useful with complex code bases, architectural discussions and also help onboard new developers to understand the solution structure and dependencies.

Here is the dependency graph for our sample source code:

You can check this resource on official website for more details. There is also a nice video on official website, which will walk you through this great feature in a simplified manner:

Other NDepend Features

Some of the notable features of NDepends are as follows:

  • Easily Manage Large Projects
  • Continuous Code Quality
  • Monitor the Health of Your Application
  • Reduce Friction Between Your Developer Teams
  • Working with Legacy Code easily

There are a lot of other useful features with NDepends and I will cover more of those in upcoming posts. Please check official website for more details and examples.

Summary

In this post, we saw that NDepend is a very useful tool and it promotes code quality in your projects. Installation is simple and you can quickly get started with it.

It has built-in rules to help you write better code and it is highly configurable to allow you add/remove rules as per your projects requirements.

I will suggest you to give it a try, download the trial version and see its ease of use and benefits to your projects and teams.

Let me know if you have some comments or questions. Till next time, Happy coding.