Tuesday, July 23, 2013

How to find the most referenced methods using NDepend 4 Professional Edition

For a while I've been writing unit tests for a framework of code we develop and maintain. For the main shared assembly I have about 40% code coverage but for the whole solution the code coverage is under 10%. My thoughts were that if I knew which methods and areas of the code we referenced the most, then I could start writing unit tests for that code now and ensure I got the largest bang for my time.

Today I posted a question on StackOverflow with the very idea and I got a response to use NDepend. As soon as I figured out how to use NDepend to get the results I want and posted it, others wanted to know how I did it.

I began by downloading a trial of NDepend 4.1.0 Professional Edition. Extract the program to a folder from the zip file you downloaded.

After a few trial and errors attempts, I found it easiest to copy the assemblies that you want to scan to a temporary directory so that it doesn't scan other non-essential assemblies. I copied my compiled assemblies with debug symbols from my solution to "C:\Temp\dump" for reference in the post.

Open VisualNDepend and I selected ".NET assemblies in folder" under the Recent Projects section.

This will bring up a dialog box for you to browse to the folder where you copied the .NET assemblies to and then press OK.

After you press OK, another dialog box will pop up. Just press OK to analyze the assemblies you've selected.

Now you should be displayed the UI Metrics Window as shown below.

In the Metrics window you will see a drop down menu in the middle of the form. You want to click the selection arrow on the right and select "# Methods calling me: 1 method - XX.X pixels" Note that XX.X will be different based on your screen resolution.

Now select the icon to the right of the drop down selection arrow that looks like some colored vertical bars and select however many methods you want to see. I selected the Top 1000 records.

On the left side of the NDepend form a Query and Rules Edit window will be displayed. At the bottom of the window are the results for the methods with the most references. At this point you can export the results however you want. I personally chose to export the results to Excel so that I could manipulate the results a little more. This isn't a perfect query just because it also lists the references to methods in the .NET Framework so I cleaned some of that stuff up in Excel.

At this point you have a complete report of the Top XX number of methods in you project with the number of times that specific method is called. I hope others found this to be useful as I did.

If you know the CQL syntax for NDepend, you could take this farther by importing your current code coverage results into NDepend and then modifying the query to exclude methods that already have code coverage but for now this works good for me.