If you have ever written an application that’s a bit bigger than a standard Hello World and that will live a litter longer than just for showing it to your significant other – I bet you’ve experience the same. You try to make it clean. You try to make it perfect but in the end it’s not as perfect as you would like it to be.

It not always your fault (deadlines & priorities) and I firmly believe that everyone is writing the best code they possibly can but over time some parts gets a bit of dirt here and the there. It might be some classes being too tightly coupled or tests that skips some parts of the method and only testing the core functionality skipping the corner cases. We all need to cut corners sometimes. I’m pretty sure you wan to keep track of those places so you start putting TODO’s and HACK’s in the source code to remind you of that only to discover (after a year!) that they are still there. Not to mention that in even medium-sized project it is impossible to have the control over the whole application codebase.
Here’s where NDepend comes into play. NDepend is a tool that helps you keep track of  some “health” metrics of you codebase. I write “health” metrics as I see multiple analogies between those checks that NDepend does and a standard medical checkup that people do.
NDepend can query your code base, process it and give you an indication of how good your code is.
Recently I was given an opportunity to try out the latest NDepend 2017.1.2 Professional and I’ve decided to give it a spin on a NetDevelopersPoland Website.

First look

When you run NDepend from outside VS (there is an option to do that from VS too) you are  presented with a Welcome screen similar to what you normally see in Visual Studio.

NDepend’s welcome screen

After creating a project we are taken to project properties where we can specify what assemblies we would like to analyse. If we want to, we can load them from our solution file but if we prefer to keep the analysis to only some of the files we can do it by manually selecting them.

Project properties

If we are happy with it, “Run Analysis on current project” button will start the NDepend processing the assemblies. After a short moment (it’s quite fast!) we are presented with the results.

Metrics

There are tons of metrics that NDepend provides you out-of-the box.

Metrics dashboard

Starting from simple ones like LoC, # of types, namespaces, assemblies and methods, to comments % coverage and Debt (which I will cover in details later). Graphs are also a crucial part of NDepend. Some issues are more clear visually than as a single number on screen. Take coupling for example – if we see that every project in our solution is connected ot every other project we clearly know that’s something’s not right. If I would see a number that describes the same situation it might not be as convincing as the picture.

Dependencies graph 

Pictures won’t lie. It’s clearly visible from this one where we should focus our efforts to reduce code complexity.

"NDepend

And if you import coverage data you’ll see a nice picture of your code coverage by tests.
NDepend - tests coverage

Tests coverage

What is also nice also nice about NDepend is that almost every item is clickable. We can drill-down and get more info about what is it that bothers or even jump into VS directly. And start fixing.
Another great approach that NDepend took is that, nothing is hardcoded. Everything you see is a result (or transformation of result) that is obtained by running a special query. CQLinq is similar to LINQ but can obtain data from NDepend data source. If we fell that something is off, or we would like to loosen the restrictions we can modify the query. After saving it we will use the new ones from now on.

Technical debt

I really love this feature of NDepend and I’ve decided to give this one a separate chapter here. As mentioned in the intro to this post, every code gets some “not-so-nice” parts. Some parts are written in such way that we are ashamed of. We promise ourselves to get rid of this technical debt but do we ever do that? NDepend can help to identify those debts and to keep track of it.
NDepend - debt info

Pay your debts!

After drilling into the Debt info we get more details. In the end to obtain detailed info about what classes are in our way of obtaining a higher grade.
NDepend - debt details

Debt details are no magic – there’s a detailed description why & how it is calculated

NDepend – Final words

NDepend is a great tool to run your project against. It really shines when you plug it into your Continuous Integration (you do have it, right?). The you get results with every build that’s done with every commit made. They you can build statistics over a period of time and observer whethere you codebase is getting better or worse.
With NDepend results it’s the same as with medical checkups. If you get some reds it doesn’t mean you will die tomorrow. It just means that your need to take care of yourself and improve on those metrics. The case with code. Few reds here and there doesn’t mean that your code is crap. Although it might be good to take care of those now when there are only few of them. Before it’s too late. Before your code will be crap. Let’s hope it will not came to that.

 

NDepend license was provided by ZEN Program Ltd.
Founder of Octal Solutions a .NET software house.
Passionate dev, blogger, occasionally speaker, one of the leaders of Wroc.NET user group. Microsoft MVP