Use NDepend to Improve your .NET Code Quality

Raymond Tang Raymond Tang 0 1143 0.31 index 6/1/2015

The first post about NDepend

First of all, I’d like to thank you Patrick (owner of NDepend http://www.ndepend.com) to provide me the professional license of NDepend. This is definitely a cool and powerful tool even I just started using it.

In this post, I am going to introduce the simple steps I used to detect my project’s code quality.

Install NDepend

Go to page http://www.ndepend.com/download and download the trial by simply providing your Email.

Follow the official instructions to install it (http://www.ndepend.com/docs/getting-started-with-ndepend).

Get familiar with the menu

Once installed, the NDEPEND menu will be added into the Visual Studio menu, where you can initiate code analysis. You can also do the same via NDepend Start Page links.

https://api.kontext.tech/resource/35a875e3-c2d5-5635-9827-67ea2e6e0d91

https://api.kontext.tech/resource/44423068-61a6-5c88-9a61-dd358cdb8c5e

Starting to analyze

Click the link ‘Analyze VS solutions and VS projects’ to start analyzing.

https://api.kontext.tech/resource/f7902043-6f3e-597a-bc0c-6333aa420e71

In the dialog, select your solution and then click OK button to continue.

https://api.kontext.tech/resource/aac0f035-24f8-5c42-b763-52122da555e2

In the ‘Analyze a set of .NET assemblies’ dialog, you can resolve any issues highlighted in red before start the analyzing as sometimes some projects cannot be loaded or there are multiple versions can be found. Normally you can Cleanup and Rebuild your solution to fix most of the issues.

Click ‘Analyze XX .NET Assemblies’ button to continue.

https://api.kontext.tech/resource/76c90df1-a6bf-5558-ad65-e14fabde25c2

In the result dashboard, you can find out the summary about your code quality, e.g. Code Rules violated cases, total line of code, comment lines and so forth. You can also create trend reports about these indicators.

I didn’t realize there are so many issues with my code.  I will definitely use the information NDepend provided to fix all of them.Open-mouthed smile

For example, by clicking one of the violated rule, I can get all the details and then fix them accordingly.

https://api.kontext.tech/resource/72498e9e-c876-5c5e-aeae-8677f4da978b

Some interesting and helpful diagrams

NDepend provides some helpful diagrams as well to help you understand your projects.

Dependency Matrix

https://api.kontext.tech/resource/f688b8fd-3cde-562a-9f78-ab11f7e08c89

Dependency Graph

https://api.kontext.tech/resource/9dec75b5-7c80-5200-b76a-b77e22d310bd

Abstractness vs. Instability

https://api.kontext.tech/resource/90acaebb-2a1d-510e-8197-5b81a30138fc

Treemap Metric View

https://api.kontext.tech/resource/ea4fa519-057a-5d47-ac3d-f3c57dd9d00f

With all these diagrams, I will immediately know where I am not not architecting or designing well (and there are a lot places to improve apparently Embarrassed smile).

Summary

NDepend helps me to analyze my code and find out all the coding issues very quickly. I am surprised by the enormous set of rules NDepend provides to validate the code quality.  This is absolutely an powerful and great tool to use if you want to improve your code quality and to adopt the preferred coding practices in .NET. I am just a beginner in NDepend’s world but I will definitely utilize it more in future.

I noticed there are some rules which are covered by Visual Studio but not appearing in NDepend’s result but it may be because I missed them, i.e. CA2241 Provide correct arguments to formatting methods. However this won’t really impact anything. I was actually thinking whether it is possible to bring all the Microsoft defined rules into NDepend?

dotnetcore

Join the Discussion

View or add your thoughts below

Comments