A while ago I was approached by Patrick Smacchia, Lead Dev at NDepend, asking if I would be interested to review their product.
I have used it since then off and on but never really sat down to fully understand its potential until a day or so ago.
I am going to focus on NDepend in a few blog postings with this one being the first.
What is NDepend?
NDepend is a source code analyzer to help you understand your code and how it is build from an architecture point of view. It does that focussing on two main areas; Code Metrics and Dependency graphing. All other features, and there are a lot to play with, fall in either area.
NDepend is a complementary tool to the Code Analysis tool build in Visual Studio. Where Code Analysis is based mostly on patterns and conventions, NDepend is based on numbers, sort of the cold-hard-facts, for example show all methods with more than 20 lines of code.
Where Code Analysis allows you to only turn on or off a rule (if you don’t like the result), NDepend lets you tweak the reporting limits which may make more sense for your type of applications.
It can be integrated into Visual Studio and it provides means to make NDepend part of your build process
Features
Code Query Language
NDepend provides a Code Query Language (CQL) tool which allows you to query your code for pretty much anything you can think of; for example: What are the 10 most complex methods, which complex method is not enough commented, which methods creates objects of a particular class and so on. Those CQL statements are also used as the rules during an analysis run. NDepend comes out of the box with about 80 something metrics rules which you can turn on or of, tweak or extend with your own metrics (no coding required).
In the image above you find the metrics categories in the middle pane and the individual metric rules in the right pane. When you select one of the rules, the CQL Results pane (left) will show you the items it selected and a small snapshot of the query. The results list is very useful, you can group the results in a number of ways, the one I like is where (in this sample) the selected methods are organized by source file.
Double click on the method and it instantly opens Visual Studio, the source file and the focus is set on the selected method.
You can edit the rule as well which has an instant effect on the results. The language feels like SQL so that is very comfortable to get into. The editor also has intellisense features, however I found that the editor doesn’t behave like Visual Studio. Once you start typing the intellisense feature pops up.
My Comment: I like to hit tab, enter or an arrow key to accept the current selected item, now you need to select an item with your mouse to continue. It does somewhat behave as expected when you enter a space before the next word, I guess a definite point for improvement.
Most of the metrics rules that ship with NDepend have a link in the rule definition, as seen above. This link will take you to the web site where you can read more about the definition and the thoughts behind it which is great.
My Comment: The one thing that would be helpful is if the offending column would be highlighted in some way. If you take a look at the image to the right you can that the query has about 7 WHERE clauses of which one is the main cause the record, highlighted in the CQL Results pane, shows up in the search results.
When you work with the CQL Editor you get some basic pointers if your query is ok or not (see image above), also if your query is correct you will immediately see the results in the CQL Results list. However, if your query is incorrect, the whole query statement is highlighted in red but no reason, at least that I could see, was given as to fix the problem.
My Comment: Since this is one of core pieces of the application, the editor should be providing more information on errors but also in intellisense operations where you would expect some description of what the function does.
CQL Results Reports
The results of a single rule / CQL statement can be exported to HTML, Excel, Text or XML. Which is a useful feature when you want to task your team to fix the problems. Both the HTML and Excel reports are nicely formatted (font sizes, colors and so forth).
My Comment: The report it generates is rather fixed and can result in a rather lengthy report. I would have liked to be able to customize the report at least a little bit, for example to show or hide the query, title or goal description.
You can also right-click on a category and export the results to an HTML report. This is the only export option which is too bad as I would expect to have the same options as when exporting from the results list.
Reporting metrics on Code Coverage
You can import code coverage files from Visual Studio or NCover to run CQL statements against code coverage data as well. This allows you to report if classes or methods have enough coverage or not.
Reporting metrics on Previous Builds
Once you have fixed all the reported issues and/or tweaked the rules to your project you want to make sure that any changed or newly developed code is in compliance with your rules. NDepend allows you to compare analysis runs based on a “base” data set. This allows you to see what has changed or added and if the “teachings” and “learnings” actually have a positive effect on the developers as you should see less warnings in your metrics.
Link to other Parts
From the CQL Results list you can right-click on any of the individual results to get more options. In the sample on the right I selected a method, from the context menu you can directly query “Who is Directly using me?”, “Who is InDirectly using me?”, “Who I use Directly?”… “Open in Source Code”, “View on Dependency Matrix”, “View on metric view” and more.
The “Who…” links build another CQL statement based on the item you have selected to get the question answered. This is another invaluable feature to get an understanding of your application architecture. If you see anything wrong and you want to prevent it from happening, you can save or create a new CQL query and have it run at the next analysis run.
I’ll discuss the Dependency Matrix in the part two of this review.