|
Recently I have been in the need of extracting kind of a "call tree" of some functions of my code.
More exactly, it was not such the call tree, but I was needing to be able to list of methods being called by a given entry point, and then seeing the size of each tree.
|
NDepend can very easily give this information. Let's see how !
Let's start with code
Let's write some code that we'll use to validate our results. We'll start with a very basic set of class that will call each other:
namespace ClassLibrary1
{
public class SecondClass
{
public void DoInSecondClass()
{
this.PrivateMethod();
}
public void DoInSecondClass_NoPrivate() { }
private void PrivateMethod() { }
}
public class TopClass
{
public void Do()
{
this.FirstMethod();
this.SecondMethod();
}
private void FirstMethod()
{
SecondClass obj = new SecondClass();
obj.DoInSecondClass_NoPrivate();
}
private void SecondMethod()
{
SecondClass obj = new SecondClass();
obj.DoInSecondClass();
}
}
}
Let's create our NDepend project
Our project is very basic but should be enough to validate our results. So now, we will create our NDepend project to start collecting (and validating) our metrics.
-
Start NDepend
-
Click on "Home / Start" and choose "New Project"
-
Give a project name ("NDepend Test" for instance) and the location where you want to store it
-
In the Code to Analyse tab, click on Add assemblies of a Visual Studio Solution and select the project you have just created.
-
In the Report tab, click on Select All
And here you are, ready to start the analysis ! Just click on Run Analysis and wait for NDepend to prompt the report.
Let's dig into the called method
We now want to find all the methods called by our entry point.
At the end of the analysis, NDepend pops up the report in your favorite browser. However, we'll close it for now and we will search our entry point in the Class Browser. To do that, either use the Show Menu button in the menu, or the hotkey ctrl + alt + C.
Just select the method you want, right-click on it, and select Who I use indirectly and then SELECT METHODS WHERE ...

Click on the image to enlarge
What happens here ? NDepend will generate for us a CQL Query. A CQL Query is written using the CQL language, a SQL-like language that allows us to run queries against our code to extract some information, statistics, .... Here we are using the following query :
SELECT METHODS WHERE IsUsedBy "ClassLibrary1.TopClass.Do()" ORDER BY DepthOfIsUsedBy
When we look to the result, it looks pretty good, unless that we do not have the number of line of code. Easy. We can just update the CQL Query like that :
SELECT METHODS WHERE IsUsedBy "ClassLibrary1.TopClass.Do()" ORDER BY DepthOfIsUsedBy, NbLinesOfCode
and here the result we get :
Of course the last column is visible only because we have altered the query to add another ORDER BY clause
Any graphical representation ?
It would be very nice to be able to extract a visual call tree with all the methods we have here in the CQL result. And of course, this can be done easily. Just go to the CQL Query Result page and right clic on the line marked 8 methods matched. Then you can choose Export 8 methods matched to Graph

Click on the image to enlarge
Antyhing missing ?
What we have here is pretty cool to find quickly information and in our case the dependencies between methods. Anyway, there is a functionality that could be nice to be added on the graph. We may start viewing a graph not from a CQL Query result, but from a list of assemblies. And thus it could be interesting to be able to :
-
From a graph of assemblies dependencies : doing a zoom meaning double-clicking on an assembly to see the type dependencies of that assembly
-
On a type, being able to do a zoom, meaning double-clicking on the type to see the method dependencies of that type
-
On a mehtod being able to right-clic to add to the graph or the callers, or the callees
Conclusion
Anyway, even if some functionalities could be added to the tool, NDepend is a very interesting tool, easy to learn and very powerful - especially due to the CQL language.
Give it a try, and comment about our use !