Understanding Code with NDepend


tags: ,
no comments

Unfamiliar Territories

Recently I had to quickly understand and take over a customer’s .NET code base. This code base has a lot of history, and as such I was sure that time has taken its toll on it. My goal was to quickly understand the overall quality of the code as well as map areas of the code base which are more problematic in regards to maintainability and testability. While it is possible to accomplish this task with Visual Studio’s Code Metrics, I wanted to get a deeper insight and that’s when I turned to NDepend. Luckily for me, NDepend offer a free developer license to Microsoft MVPs, but even without being an MVP the tool is available with a free trial period allowing you to try before you buy.


Ready, Set, NDepend!

So what is NDepend? According to Wikipedia:

NDepend is a static analysis tool for .NET managed code. This tool supports a large number of code metrics, allows for visualization of dependencies using directed graphs and dependency matrix. The tools also performs code base snapshots comparison, and validation of architectural and quality rules. User-defined rules can be written using LINQ queries. (…) Code rules can be checked automatically in Visual Studio or during continuous integration.

When opening NDepend, you are offered with several possibilities for choosing assemblies to analyze. The most convenient option- opening a VS solution – is conveniently highlighted (the arrow appears in the source).

NDepend on Open

Then, once the VS solution is selected NDepend goes ahead and analyzes your code base, providing you with a complete report of what the state of the examined code base. This report is available as an HTML report which you can send to your colleagues, as well as a fully interactive, queryable version with the Visual NDepend application.

NDepend Report

NDepend Report (taken from NDepend.com)

Abstractness vs. Instability

There are way too many options and reports to explorer than I can write about here, however I would like to take a closer look at one interesting feature – the “Abstractness vs. Instability” graph. While this graph is a little hard to grasp at first sight, it actually provides a very unique insight on your code. It is an insight on how hard will it be to modify and extend the various parts in your code base going forward. It is composed of two axes:

  • Abstractness – abstractness is relatively easy to understand. It is a metric measuring how much of your code is composed of interfaces, abstract classes and abstract methods, which can be extended and modified without changing the actual code or module.
  • Instability – Instability is a bit harder to grasp. Basically, the more assemblies and types are using your module, the more stable it is. I.e. an unstable module is a module which does not have many dependencies on it. On the other hand, a stable module has many dependencies on it so it is hard to change its public surface without breaking anything.

When put together, this diagram looks something like this:

Abstractness Vs. Instability

This diagram helpfully highlights two areas which you don’t want to be in (highlighted in red):

  • Zone of Pain – assemblies which are not abstract enough on the one hand and used everywhere on the other hand are very hard to maintain going forward. If you have assemblies here, you better consider doing something about them (and refactor).
  • Zone of Uselessness – assemblies which are very abstract, and have lots of interfaces in them, but are hardly used are, well… useless! You don’t want to be here either.

As long as you stay in the green area (or close to it) you’re good to go. As you can see, in the code base I had to examine the situation was rather mixed, with some assemblies in the green area while others and gearing towards the dreaded Zone of Pain. After looking at this diagram, I could get my priorities straight and start looking at the suspicious assemblies first. Now that’s what I call efficiency!

By the way, NDepend has a UserVoice site for voting on new features. If like me, you think that the Abstractness vs. Instability is pretty cool, then I’d appreciate it if you could go ahead and vote for this feature. You will make at least one software architect very happy… 🙂

Try It Out

NDepend is a very nice tool to learn and keep in your toolbox as a .NET developer. It is available to download from here. Go ahead, give it a try…

This post was originally posted on Stiller on Software
Add comment
facebook linkedin twitter email