juin 12

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 !

juin 12

A few months ago, Patrick Smacchia offered me a professional licence of NDepend so I can try it on my current projects.

I was already knowing NDepend as being a reference tool for analysing code, extracting metrics, ... but I never had the opportunity to really trying it. But I was so far from the truth !

Unfortunately, I have had very hard months with many things to do (day to day job, articles, conferences, ...) and I have had to postpone so many times my trials.

But better late than never ! It's now on my top priority list and all the trials I now do with the tool show me the extarodinary possibilities of the tool. I will explain in a next post what was my first use of NDepend.

Ready for a try ? Just download a trial version of the tool and have fun !

juin 05

As I explained some time ago, I was writing an article about Continuous Integration in the Microsoft.NET world for the (french-speaking) website http://www.developpez.com/.

The article has been phased as follows :

First Part (published on March 4th - available online here) - 43 pagesSecond Part (published on June 5th - available online here) - 62 pages
  • Quick introduction to continuous integration
    • Why do we want Continuous Integration
    • What is Continuous Integration
    • What do we call BVTs
  • Quick presentation of the tools needed for Continuous Integration (in the Microsoft.NET world)
    • MsTest
    • Static Code Analysis
    • MsBuild
    • TfsBuild
  • Unit Test : Writing unit tests with MsTest
    • Structure of a test class
    • Checking the correctness of a test
    • Testing the non public API
  • Unit Test : Executing tests with MsTest
    • Via Visual Studio 
    • Thru Command line
  • Unit Test : Configuring tests
    • Test Runs naming
    • Code Coverage
    • Deploying files
    • TimeOut
  • Unit Test : Managing the tests
    • Via the "Test View"
    • Via the "Test Editor"
  • Unit Test : Additional attributes
    • Behavioral attributes
    • Informational attributes
  • Static Code Analysis : Presentation
    • Activate the analysis
    • Running the analysis
    • Explication of the rule naming
    • Parameterizing the analysis
  • Static Code Analysis : Correcting the errors
    • Correcting one error
    • Correcting several errors
    • Grouping the rules suppression in a global file
  • Static Code Analysis : Limitations
  • Quick introdution to MsBuild
  • First steps with MsBuild - Writing a basic project file
    • Its structure
    • What is a task ? a target ?
    • Creating and executing a project file
  • Going further with MsBuild
    • Ensure the project file is valid
    • Properties and PropertyGroup
      • How to statically & dynamically create them ? To use them ?
      • Order of declaration
      • Updating the value
      • Delaying the creation of a PropertyGroup
    • Items and ItemGroup
      • Same detail as for Properties
    • Executing a project file in command line
      • Choosing the target to execute
      • Overriding properties
      • Playing with verbosity
    • Using .NET 3.5 tasks
    • Playing with Targets
      • Control the execution order
      • Explicit call of a target : 3 techniques and their differences
      • Passing input and output parameters to a target
  • Refactor a project file
    • .targets files
    • Importing files from Source Control
  • Create your own tasks
    • A simple one, with inputs (mandatory or not), with outputs
    • Raising errors
    • Debugging your task
  • Quick introduction to Team Foundation Server
  • TFS & Continuous Integration
    • Create, edit and execute a build
    • Parameter a build
    • Which target can you override ?
  • Managing TFS
    • Destroying, Undoing and listing files
    • Presentation of TFS Power Tools
  • Tips & Tricks

To read this article, just go to http://dotnet.developpez.com/ or directly to my webpage on this site : http://pedautreppe.developpez.com/.

Note that these article are available only in french for now, but do not hesitate to leave comments here if you think the content is interesting and that it could be interesting that I translate it.