Continuous Code Quality using NDepend in Visual Studio

code quality using NDepend

Today in this article, we will learn how to improve code quality using NDepend using Visual Studio. Most of us understand the importance of code quality.

Good coding practices are language agnostic and help any organization deliver clean, highly reliable, secure, and maintainable code.

Today in this article, we will cover below aspects,

Why Code Quality is Important

  • Software maintenance is like a big fat elephant and often eats away a big chunk of project budget allocation.
  • Re-engineering and refactoring of buggy code cost organization time, cost, and quality.
  • Today’s small-looking coding issues become bigger problems in the future and potentially affect the complete architecture and design of your application.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand”  -Martin Fowler

Today in this article we shall see how to use the NDepend static code analyzer and IDE extension in Visual Studio VS2019. NDepend supports all versions of Visual Studio.

NDepend is a static code analyzer and comes as an IDE extension that helps to detect and fix code quality issues as you type the code in IDE.

Main Features/Characteristics Developer might like

NDepend has many exciting features and addresses a dozen use cases which we will discuss in today’s and future articles.

Below are a few basic features that might be of interest to developers.

  • Easy integration with IDE:

The great part of NDepend is the easy integration with Visual Studio which helps developers identify the coding issue as they type their code.

  • Easy integration with DevOps/CICD

Easy integration with Server-based reporting and CI-CD quality gates. NDepend also gets integrated with Jenkins, TeamCity, ADO (Azure Develops), Sonar Server (with a nice dashboard of all project portfolios), and Coverage tools like Open Cover, NCover, etc.

code quality using NDepend

  • Customized Coding rules

A developer can use default coding rules or can write their own custom rule with a simple and easy-to-understand syntax.

  • Track your code health

The developer can easily track the code health by tracking many code-specific trends like lines of code, coding issues, coding rules violated, Code coverage, etc.

There are many other features supported by NDepend like,

  • Smart Code reviews,
  • Finding API breaking changes,
  • Finding Dead code in the project,
  • Smart Technical debt extermination
  • Architecture diagram ex. Dependency graphs.
  • + Many more

NDepend supports the below languages for static analysis,

  • C# (.NET, .NET Core, .NET Standards)
  • VB.NET-
  • C/C++
  • Java

Prerequisites

Before we get started, please make sure you have the below basic things ready,

  • Download the NDepend Installation file from the NDepend Website.
  • Visual Studio 2015 or 2017 or 2019

Getting started

Step 1: let’s get started with installing the NDepend installation file.

UnZip the installation file to any choice of your folder and click on the NDepend.VisualStudioExtension.Installer.exe

install NDepend

Note: Please use any custom folder instead of using the C:\Program Files directory for extraction and installation purposes.

Step2: Install using Installer

Please click on the installer file NDepend.VisualStudioExtension.Installer.exe

If you don’t have a license key, no worries you can still evaluate the software for 15 days, verify and examine its promising capabilities.

Otherwise you can enter a license key, activate it, and complete the installation.

code quality using NDepend

Please install the software for all of the required Visual Studio versions. After successful installation, you shall see the below message,

Code Analysis of Source Code

Now we have installed the software successfully.

Let’s start analyzing the project or solution.

In Visual Studio Menu, NDepend will start showing up in the Visual Studio Menu bar as shown below,

Please click on the “Attach new NDepend Project to current solution”.

Once you click on Analyze .NET Assembly, you shall see a nice dashboard with a lot of details giving you a real insight into the health of your code.

Below are the main details of your code health quality metrics which are displayed on the Dashboard.

@1 – Dashboard measures the size of your code showing detailed lines of code.

@2– The dashboard measures the size of your code and provides details on the number of Types, assemblies, methods, and Fields your project contains.

@3- Dashboard shows the Technical dept and overall Rating for your code from the technical dept of view. The more the technical dept bad the rating you get.

Example: Categories are indicated as A, B, C, D, and E, where A is the minimum (1%-5%) and E is for over 50% indicating the bad quality of code.

@4– Code coverage report for the code you have written if configured.

@5 – The dashboard shows the quality gates configured and quality goals that must be enforced while doing any commits to the projects. It can be used as a measure to indicate Pass/Fail criteria for the given commit or release.

@6- Rules types and counts of rules violated that are validated

  • Critical
  • Violated
  • OK

@7- Overall coding issues are categorized based on the severity

  • Blocker
  • Critical
  • High
  • Medium
  • Low

Validating Coding Rules

All the above dashboard metrics are clickable and once you click on any metrics you get to the details of the metrics including rules violated and the line of the code where the issue occurred.

Please wait !! There are many more features of NDepend support that could be of interest to you.

I shall be covering many of these developer-centric features in my next articles! Until then please stay tuned!

You can download and try the trial version from this link.

References:

Happy coding !!

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Summary :

Today we learned about how to get started with NDepend – a static code analysis tool. We learned how to set up it for Visual Studio IDE. Producing continuous good quality code is important and has a significant impact on the project life cycle and its maintenance cost.

Organizations can address Code quality problems by following best practices of software development and opting for the best fit COTS product like NDepend for addressing many code quality-related use cases.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *