I’ve wanted to take a look at NDepend for quite a while now. So today I am going to write about my initial impression of this excellent static-analysis tool. To get an immediate feel for what NDepend is and what it does, there is a short video that can be viewed here.
Managed code comes with many advantages. And one of the most significant (possibly the most significant) is the mountain of meta-data that .NET assemblies carry about themselves, and those that they interact with. This plethora of information can be tapped, analysed and presented in a meaningful way which gives coders/architects a great, bird’s eye view of the state of a given code base.
This post will not be a tutorial. But it will contain screenshots and some explanations. It will fall short of being a tutorial, because I am not setting out steps from end-to-end. Think of it as a summary of one developer’s experience analysing a real .NET solution with NDepend. The solution that I will analyse is my Home Library sample application (which is nowhere near finished and currently in a state of rampant architectural experimentation).
The first thing I wanted to check out once I had run the analysis on my solution (including all of its projects) was the
metrics stuff. That looks interesting to me and I immediately decided to hone in on the
Most Complex... - Types metric. When you select that from the NDepend menu (NDepend > Metric > Most Complex… > Types), it loads 2 separate panes:
- Search Results pane
- Metrics pane (which shows a treemap of the results)
Once I got my head around what I was looking at, it occurred to me that it was showing me the same information, but through different prisms:
Figure 1 – Metrics and Results Panes
Looking carefully at Figure 1, we can see that the way in which that information was retrieved was by way of the query that can be seen in the
Search Results pane. Now that is pretty awesome, because NDepend has its own version of LINQ called CQLinq, which is used to pull out data from the static analysis (I am not sure if it is a fully-fledged LINQ provider, as I haven’t seen the word “provider” in any of their doco – but still über cool to have your own flavour of LINQ). Their documentation about syntax etc is a very good reference for writing CQLinq.
If you hover over the various methods in the tree-view (at the foot of the
Search Results pane), you will see the corresponding rectangle in the treemap light up. You can see in Figure 2 that
I have honed in on the
ExpressionBuilder type, as I do recall that carrying quite a bit of complexity. By the size of the rectangle in contrast to the rest of the treemap, that complexity is represented well. Obviously, the UI-related types in this WinForms app have the most complexity, each with quite a large
Initialize method for all the controls on the forms.
Figure 2 – Hover Cause and Effect
Another excellent feature in the
Metrics pane is a drop down list which filters the top n items which match the query. In fact, if you look over to the right in the query window, you can see
Take(10) has been added to the CQLinq query in Figure 3:
Figure 3 – Top n
I was correct in my feeling about the
ExpressionBuilder class, as it is featuring in that top 10. If you click on the
Edit Query button in the
Search Results pane (visible in Figures 1 and 2 above), it launches a pane with a text-box for query manipulation. You can then add a new query, which is a pretty cool extension point. But that’s just the tip of the iceberg when it comes to customising NDepend to your needs.
The other feature that I wanted to highlight, as something that I found really cool, is the ability to see
Dependency Graphs of your code. Figure 4 shows a graph I created from the NDepend menu NDepend > Graph> View ApplicationAssemblies Only:
Figure 4 – Dependency Graph Assemblies
That’s a really nice representation of my architecture (just as I envisioned it). You can see the UI stuff towards the left, and then as you move through the middle (the domain), you eventually hit the data stuff on the right.
You can also create one for namespaces. Check out Figure 5 below:
Figure 5 – Mutually Dependent Namespaces
I’ve highlighted a problem there, which the tool found, regarding mutually dependant namespaces. The tooltips (the light-yellow boxes) provide some information to assist me in hunting down the reason for this unseemly coupling issue. This is a very valuable view of the relationship between your objects. You can also drill down further by right-clicking on the connector “edges” between the nodes in the graph to look at member dependencies etc.
The really cool thing about those tooltips is that they are dynamic. When you change a query or selected-item in a drop-box, the data displayed in those tooltips changes before your very eyes.
There are a heap of other things NDepend offers, some of which will have to be the subject for a future post.
But I’ll leave the reader with this – the developers of NDepend use NDepend to develop it. To use their words, they eat their own dog-food. And a very high quality kibble it is.