Code metrics and more with NDepend
Do you know the quality of the project you’re currently working on? Sure you can have guidelines and rules… but projects where a few people have worked on for a few weeks/months/years tend to live their own life here and there. Maybe the team had to catch a deadline for the latest sprint, or the lead developer was on holiday and the problem had to be solved using a quick and dirty solution, … Every project will have its own little exceptions to the rules and guidelines.
But it should be possible at a certain point in time to measure how good (or how bad) the project is being developed. And this is where NDepend comes in. Quote from the NDepend site:
NDepend is a Visual Studio tool to manage complex .NET code and achieve high Code Quality. With NDepend, software quality can be measured using Code Metrics, visualized using Graphs and Treemaps, and enforced using standard and custom Rules.
Hence the software design becomes concrete, code reviews are effective, large refactoring are easy and evolution is mastered.
To install NDepend you just download a zip file, install it, copy your license in the root of the directory (if you have one) and you’re done. Afterwards you have to go and read QuickStartTutorial_NDepend.txt to see how you can install the Visual Studio addin. This is a personal choice but I’m not too fond of XCOPY deployment. I would rather have had a regular setup wizard where I could just check or uncheck the Visual Studio 2010 integration.
Patrick Smacchia (the creator of NDepend) wrote a nice article regarding why he chose XCOPY deployment: http://codebetter.com/blogs/patricksmacchia/archive/2009/08/10/the-benefits-of-xcopy-deployment.aspx
Now that I installed the application and the AddIn we can continue.
Mode 1: VisualNDepend
In the NDepend directory you’ll find the file VisualNDepend.exe, this is the standalone application.
Since NDepend uses DevExpress as control library you can easely make a few changes to the UI. I decided to go for the black ribbon, here is the result:
In the rest of the article I’ll be using the standalone application.
Mode 2: NDepend AddIn
NDepend also integrates with Visual Studio 2010. It doesn’t seem to provide extra functionality but it integrates very well with the solution you’re currently working on.
Also when you right click a project you get tons of functionalities that directly apply to that project:
Analyzing a real project
I’m currently working on an ASP.NET project that was completed about a year ago. The goal here is to start refactoring so that we can easily move to Silverlight in the future. NDepend is based on analysis of assemblies, but when you create a new NDepend project you can also open a Visual Studio solution which will in turn add all projects as assemblies to be analysed.
After loading the solution the external references will also be resolved automatically. And that’s it. Now you can hit the button Run Analysis to get started. After a few seconds / minutes the analysis will be done and you’ll have a complete overview of your solution.
The first thing you’ll see is an HTML report containing all the information:
- Overview of the application metrics
- Overview of the assembly metrics
- A VisualNDepend view
- An assembly dependencies diagram
- And much much more!
The fact that the report is generated automatically is great (we’re working with SharePoint to store all our documents). If you’re working with a team at your customer you can easily share the analysis report with the local team.
Code Query Language
NDepend introduces the concept of Code Query Language (CQL). This language allows you to create reports of the analysis as if you were working with SQL.
WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE
CyclomaticComplexity > 20
ORDER BY CyclomaticComplexity DESC
Just like Visual Studio comes with a standard set of snippets, NDepends comes with a standard set of queries (more than 100!).
Analyzing the output of a query
The set of standard queries contains a lot of interesting queries you can use for each project. One of the most interesting is the query Do not raise too general exception types. It’s true that when you want to do some quick tests or you’re not that experienced with exception handling you tend to use Exception a lot. But it is a best pratice to use your own type (for example CreditLimitReachedException) instead of the default Exception / ApplicationException.
When I ran the query NDepend provided me with the list of methods that were using these global exceptions:
After double clicking a method NDepend will bring up Visual Studio, open the right file and jump to the right line. You can start fixing your code right away!
In the introduction I talked about rules and guidelines. Well, thanks to CQL the person in charge could create some queries that check if the rules and guidelines are followed correctly.
An other great functionality of NDepend is that you can track your code coverage. When you’re writing unit tests you want to be sure that your tests cover all the code. NDepend will give you a visual overview of all your methods and their coverage and you can even create reports. This is useful for when you want to find all methods with a coverage percent less than 50, create a report out of this data and sent it to your test team:
I covered a few interesting features of NDepend but there’s still much more to discover:
- Dependency Graph
- Dependency Matrix
- Visual Metrics
- Comparing with previous analysis (to see how the project evolved)
- MSBuild integration!
Blogs and documents:
- Introduction to CQL
- Write Custom Design and Quality Rules
- Write Custom Smart Naming Rules
- Rationalize my multi-threaded code
The only alternative to NDepend I know is Microsoft’s FxCop. A great feature of FxCop is its integration with TFS.
- Download: http://www.microsoft.com/downloads/details.aspx?FamilyID=917023f6-d5b7-41bb-bbc0-411a7d66cf3c&displaylang=en
- Code Analysis Blog: http://blogs.msdn.com/b/codeanalysis/
NDepend is a great application with an unbelievable amount of features. Everything you see in the application can be exported one way or the other (be it HTML, PNG, XLS, …) which is great. Seeing visual representations of the analysis will also help you get a better view of things. And with CQL you can really enforce your company’s own rules and guidelines throughout multiple projects. The code coverage support is great and I truly love the MSBuild integration.
I’m a happy user!
And finally since I’m a big MSBuild fan I might write an article in the future that handles the integration of NDepend with MSBuild/TFS/TeamCity/SharePoint…