Analyzing Your Code with NDepend

On your way to becoming a great programmer, you might ask yourself from time to time if the code you have written is actually good code.

Is it robust and maintainable? Does it follow best practices? Are there any parts that might cost you a lot of sweat and tears in the long run?

Fortunately, you don’t have to answer all these questions by yourself. There are tools that do that for you. And one of these tools is NDepend.

I was lucky to get a glimpse at the latest pro edition of NDepend – thanks to its creator Patrick Smacchia (this is NOT a paid endorsement!) – and fired it up to check a quite small Visual Studio solution – the source code for an online course – and a bigger solution – an indie game I’m currently working on with Unity.

In the beginning, you already notice, that the analyzing features of NDepend are pretty deep. And I barely scratched the surface.

The dashboard already provides a sweet overview of your whole codebase.

150 lines of code (as I said, a quite small solution…), perfect A rating, but what’s that? A critical rule violation? What does that mean?

A click already tells me everything. And that’s a really great part of NDepend: the documentation. Everything is explained in detail and there are also links provided that go even deeper.

I can see the debt of this issue, annual interest, the breaking point and also a detailed description of this violation and how to fix it. A great opportunity to learn that stuff and get better at my craft.

As soon as I fix this issue, I can let NDepend analyze the project again and it tells me the differences to the last analyzation. Did the code quality actually improve or not?

Now let’s have a look at the bigger project. I can either open the solution in Visual Studio and analyze it with the NDepend extension or open NDepend itself and let it analyze the solution there. Either way, I see that the project has some more lines of code and, of course, a lot more issues.

Funny thing though, a lot of issues actually come from assets I got in the Unity Asset Store. You can easily see that in the code metrics view. Just look at this beast.

Hm.. maybe they should analyze their code, too.

Apart from that view, you can also have a look at a dependency matrix and a wide variety of graphs.As already mentioned, I’m really just scratching the surface here.

Also, you can define the rules for every issue by yourself. These rules are actually defined in C# code. That’s neat!

To get the most of this tool, you have to spend a lot of time and learning. But if you do so, it can result in great benefits. One thing I had to struggle with is the usability of the tool. Sometimes it wasn’t that easy to get where I wanted to, the back button on my mouse did not work as expected and I wish there was an integration of NDepend that would tell me the issues directly when I work with my actual code – by using some icons on the left, for instance. Or maybe I’m just missing something here?

On the contrary, of course, there is this endless list of features. With that list, you may find issues you would most likely never find without NDepend. Every issue is explained in detail and you can learn to fix all issues with the great documentation provided.

Even Scott Hanselman likes it:

“NDepend is giving me insight into my apps that I hadn’t had before. Once I realized the depth and breadth of the information I was looking at, I was like a kid in a candy shop.”

To sum it up, I think NDepend is a tool for single users or small teams that really want to dive deep into their codebase and analyze the heck out of it. If it’s about debt and deep quality – and you have the time for that – this is for you.

It can already make sense for small projects but might be more useful for really big projects where you have lost track of your codebase. This case is probably more likely to occur in larger companies which could well use NDepend then.

To really get the most out of this tool, you have to deal with it more deeply and for longer. It’s not just installing, watching the graphs and forget everything. But if you do that, you have the chance to improve your code quality dramatically – and learn a lot on the way.

If you need a tool for quick tips and tricks while you are actually coding, other products might be more suitable. You may want to have a look at ReSharper or simply use the built-in features of Visual Studio. They are often underestimated, although they’re already quite good!

Header Image Designed by Freepik

2018-03-19T12:35:27+00:00 March 19th, 2018|Blog|