Posts Tagged ‘NDepend’
Recently I got the new version of NDepend. Thanks to Patrick Smacchia and team to build such a wonderful tool for the developers.
In one of my previous post I explained the features of NDepend and then after NDepend become the most important tool to make my code healthy and maintainable. One of the NDepend feature really knocked me out is CQL, query the code using SQL like syntax. That was a new experience to me, in my last post I wrote a CQL to find the dead functions in my application as shown below.
SELECT METHODS WHERE
MethodCa == 0 AND !IsPublic AND !IsEntryPoint AND !IsExplicitInterfaceImpl
AND !IsClassConstructor AND !IsFinalizer
Isn’t it cool if we can write these kind of queries in Linq. Yes we can, with the latest version of NDepend V4 we can write Linq queries to query our code and is called CQLinq. Let’s see how we can rewrite the above query using CQLinq.
from m in JustMyCode.Methods where m.MethodsCalled.Count() ==0 && m.IsPublic==false && m.IsEntryPoint==false && m.IsExplicitInterfaceImpl==false && m.IsClassConstructor==false && m.IsFinalizer==false select m
How about the query writer supports intellisense and integratd tool tip documentation? Awesome… I love these guys. Previously I need to remember the keywords like IsEntryPoint/IsExplicitInterfaceImpl etc. Now intellisense will show all the methods and properties. Also tool tip help tells us what is the use of the selected method or property. Cool you don’t have to remember every thing, less work for brain 🙂
See the screen shot below.
If we want to add our own rules and the UI want to show the warning symbol if the query returns value then add warnif as shown below.
// <Name>Dead Methods</Name> warnif count > 0 from m in JustMyCode.Methods where m.MethodsCalled.Count() ==0 && m.IsPublic==false && m.IsEntryPoint==false && m.IsExplicitInterfaceImpl==false && m.IsClassConstructor==false select m
The Query and Rules explorer window will show the warning as shown below
I just covered very basic of NDepend’s CQLinq. You can find more detailed feature list of NDepend V4 and example CQLinq code from NDepend
This post is a very brief overview of NDepend, a static code analysis tool. In this post am just touching some key feature of NDepend. I didn’t have much idea about NDepend until Patrick Smacchia introduced it to me recently. After I got the pro license I did a quick glance of NDepend, I felt like it’s a code scanner for developers just like body scanner for doctors :).
Installation of NDepend is very simple, download the zip from NDepend site with the secret code. Just unzip and drop the license xml file, that’s it. Tools provides a VS addin as well. If you want to add NDepend to VS IDE then install NDepend.Install.VisualStudioAddin.exe. Open the Visual Studio and you can see the NDepend Menu.
I thought of writing this blog after going through the features of NDepend. First I thought I will analyze any of my existing code but I didn’t feel comfortable in analyzing old codes. One reason is, I might have forgotten how the function calls and the dependency structure. So I decided to write a new application and run NDepend on it, so that I can benchmark it better. I wrote a Twitter Public timeline reader for WP7 to test NDepend. You all can see the details of that project in my previous post.
Now it’s time to go through the code analysis using NDepend. The time to gather the data for code analysis is pretty fast, NDepend didn’t make me wait for long. The first thing I wanted to see is the Dependency Graph, below is the graph it generated
You might be noticed that some arrow is in different size, this is what the context sensitive help of NDepend says
On this graph, the thicknesses of edges are proportional to the strength of coupling in terms of number of members involved
How cool it is, by just looking at this graph I can visualize how much is the coupling between the assemblies. Clicking on the arrow will give you an info window on the right side and will provide the details of the coupling.
One of the excellent part of this tool is, it have a very good help, you will never get stuck any where. For e.g. below is the class browser of NDepend
By clicking on the assembly in the NDepend class browser will display the details about the assembly. You may not get each and every details, don’t worry all those labels are hyperlinked to NDepend site and they provided decent help document.
One of the well know feature of NDepend is Code Query Language (CQL). It’s a new experience for me to write a SQL kind of query against assembly. For E.g. I can write a query like below to find out the unused methods in my project
SELECT TOP 20 METHODS WHERE
MethodCa == 0 AND
!IsPublic AND !IsEntryPoint AND
AND !IsClassConstructor AND
When you run NDepend against your assemblies it will generate a standard list of queries to analyze your code. If you feel like it’s not sufficient then the tool provided the flexibility to you to write your own CQL query. It provides a very good query editor with intelisence. You can go to NDepend site to get more help on CQL.
I was so amazed about this query functionality of NDepend, I just wanted to know how the NDepend query the assembly. My search leads to Patrick’s reference about Cecil, one of the open source project to inspect libraries in ECMA CIL format. With the help of Cecil they implemented the CQL, a wonderful piece of functionality in NDepend.
Its a very handy feature of NDepend, it will give you matrix structure of the Assembly dependency as shown below
In this matrix I can see dependency in a matrix format. I clicked on the intersection point of TwitterApp and TwitterClientAPI (TwitterApp is on left and TwitterClientAPI is on the top). As you can see the tool give me a plain english description that 5 Methods of TwitterApp is using 11 members of TwitterClientAPI. I was curious and wanted to see which are all the methods of my TwitterClientAPI.TwitterCommunicator class is used by TwitterApp.
I only wanted to do is just expand the TwitterClientAPI on the top and again expand the TwitterCommunicatorAPI as shown below. The matrix shows that TwitterApp is calling GetPublicTimeLine function ones, yes its true. Same like it will display for other functions as well.
As I told earlier in the post, the tool also provides a pictorial Dependency Graph representation. You can get the same dependency graph from the Matrix as well. Right click on the intersection (Colored columns) and say Build a Graph, pretty simple. Below is the screen shot of the Dependency graph of TwitterApp with TwitterCommunicator class
Dependency Matrix provides a more in depth graph. From the screen shot you can see the depth of the description it provides. I don’t think I need to give any more explanation of the above graph. NDepend made your code analysis smooth and simple.
As I told earlier I just scratched the tip of an iceberg. In this post I left behind so many other features of NDepend, I will add more post about NDepend later. As Scott Hanselman said in his blog Exiting the Zone of Pain, yes NDepend is really a pain reliever. Investing some money to acquire this tool is worth. NDepend have a free trial version for open source projects and Academic purpose. Visit NDepend site for more details.