NDepend is a code quality tool that you install as an extension to Visual Studio. The product uses various metrics to tell you what kind of perceived quality your code is in, such as cyclomatic complexity (CC), lines of code (LOC), method count within types, and the list goes on. Cyclomatic complexity is a term thrown around that fairly new programmers might not have heard before, so let's do a brief explanation of it.

Cyclomatic complexity

When we talk about CC we often do it in the context of sections of code, for instance, a class or a method. And within that section of code, the complexity is measured by the number of linearly independent paths inside the section. If a section contains no control flow statements, the complexity would be 1 as there's only one code path. As soon as you introduce an IF statement there's now another code path added, bumping the complexity to 2. I hope this resonated with you, and if not NDepend has its own definition of it.

Diving into the tool

With that out of the way let's talk about the tool itself. I found the installation to be swift and easy to follow along. Once installed you can find the tool in the extensions tab inside Visual Studio and then add NDepend to your project from there. Running the code analysis didn't take long at all (this of course would vary depending on the size of your project) and establishes a baseline for your project. This baseline is used next time you run a code analysis and is used to tell you if your project is heading in a good direction or not (quality wise). My initial analysis revealed several code "violations" categorized by severity and type, as shown by the image below:

Figure 1

These application metrics gives you a great overview over the state of your project. In the top right corner you can see a heading named "Quality Gates". You can think of quality gates as rules you can define to enforce a certain level of code quality before allowing the code to enter production. NDepend comes with a preset list of rules which you can alter as you wish. This gives you great control because you can decide yourself what's "good enough" quality in your product, and halt pull requests or devops pipelines if your goals are not met.

As you can see I have a failed quality gate in my soloution, let's dive a little deeper and see why it  failed. When i click on quality gates im presented with a overview of the gate names and which failed/passed (as seen in figure 2).

Figure 2

In my case the failing gate is called "Critical Rules Violated". This are rules NDepend has marked as critical by default. When creating your own rules you have a option to also mark them as critical, which adds them to this quality gate. Let's go down another level to see what rules I'm breaking.

Figure 3

So figure 3 shows 6 rules that are broken and the one I'm breaking the most are "Avoid methods too big, too complex". Diving deeper into that specific rule shows us what methods are in violation, and their stats in terms of LOC, CC, complexityScore etc (shown in figure 4).

Figure 4

Not only do you get a good summary on a per method basis, but NDepend also gives you information about the values and thresholds that your method violated, and last but not least they provide tips on how to fix them. If we click the "View Rule Description" dropdown, we get all the info I just mentioned. And for this specific rule it looks like this:

Figure 5

As you can see NDepend provides an explanation to why your method was caught by this rule and also some helpful tips of how to recktify the situation. In the bottom section you can see that there's also a possibility of suppressing the rule violation by decorating your method with an attribute. This could be used if you believe your method needs to be in a certain way and you can justify it.

Wrap up

I've only scratched the surface of what NDepend is able to do, things I've left out include things like: Dependency graphs, Code metrics treemap, CQLinq (Code Query Linq), and Technical-Debt estimation. However I already feel like this could be a valuable tool to help any team or individual to ensure their code quality is at a desired level. The tool shipps with over 200 default rules built in and a easy way to customize or suppress them as needed.

I hope I was able to give you a sense of what this tool is capable of delivering and if you're interested in learning more about NDepend they  have a good documentation and getting started guide here: https://www.ndepend.com/docs/getting-started-with-ndepend

If you have any thoughts or comment please leave them below.

Happy coding!

Disclaimer: Patrick from NDepend reached out to me and asked if this was something I would consider writing a post about. I hadn't heard about NDepend before, so I was intrigued and decided to use this as a learning opportunity. My thoughts and opinions are my own, and I was not paid to write this blogpost.