Blog Home  Home Feed your aggregator (RSS 2.0)  
NDepend - Manuel Abadia's ASP.NET stuff
# Friday, February 8, 2008

One of the tools I’m using is NDepend ( It is a cool application that can be used for a lot of things, as will be detailed later.

To use NDepend you have to feed it with a set of assemblies. After that, it will analyze those assemblies and their reference assemblies. If the PDBs of the assemblies are available it will use them so more data is analyzed, although the PDBs are not needed for NDepend to work.

After the analysis has been completed, NDepend looks like this:

I have used version 1.1 of Spring.NET Framework ( for the sample. As you can see the application has a beautiful GUI. I’ll talk a bit about the main windows of the tool:

• Metrics: this window gives a graphical high level view of the project. You can see a lot of ellipses and some of them that are made of smaller ellipses. The big ellipses are types of the project. If a big ellipse is made of several smaller ellipses, those smaller ellipses are the methods of the type. The types of the same namespace are distributed in the same rectangular region, separated by a small yellow line. The assemblies are separated by a thicker yellow line.

By watching this window you can get an idea of which assemblies contain more code, which types are big, which types have a lot of methods, etc. The window was in “Method Level”. The available levels are Assembly, Namespace, Type, Method and Field.

• Class Browser: this is where the assemblies of the project and its referenced assemblies are shown. You can expand the assembly up to the field level. While you hover over an item in most windows, the Metrics window gets updated highlighting the hovered item.

What it’s really cool about the Class Browser is that for the referenced assemblies, only the namespaces, types, methods and other items that are used by the project are shown.

• Info: When you hover over an item the information about it is displayed here, with some metrics if applicable. There are a lot of metrics so it can take time to interiorize them and judge their value appropriately.

• Dependencies: this window is a bit intimidating at first but like a lot of things, with a bit of effort you can master it. The window displays very important information of the dependencies in a matrix form. As in other windows, the detail level is selectable, from the assembly level to the field level.

A cell can have 3 colors. Blue means that the associated item in the top header uses the associated item in the left header. Green means that the associated item in the left header is using the associated item in the top header. Black means a cyclic dependency. The blue and green cells express the same dependency using opposite points of view.

You can see a diagram of the dependencies clicking on a cell. For example, if you click in the cell corresponding to System.Web.Extensions and System.Web you get this:

To read more about the theory of dependencies take a look here:


• CQL Queries: CQL stands for Code Query Language and it is a central piece of NDepend. Being able to ask questions about the code is great. If you like using the Analyzer window in Reflector you’ll love this. You can create new queries and modify the existing ones.

I have presented the main NDepend windows, but the main question is: what you can do with NDepend?

There are several uses that I can think of:

• It is an excellent tool to check differences between different builds of an assembly. You can use it as a diff tool, as it will show in the Class Browser which types, methods, etc have been added, removed or changed. This information is also available for CQL Queries, with the power that it implies.

• It can be used to understand code. When you are thrown with code that you have no clue about how it works you have several options to accomplish that task:

  1. Look at the source code directly.
  2. Make some diagrams from the source code (for example, using View Class Diagram in Visual Studio).
  3. Use NDepend.

These methods are not exclusive, and probably you’ll use a combination of them to understand how the code works. However, the abstraction level is higher when you use NDepend is higher than when you use a Class Diagram (and a Class Diagram is a high level view compared to raw source code).  I’d use NDepend to have a global vision of the main assemblies, type most used, dependencies between namespaces, more complex methods, etc. and then focus my attention in the most important parts of the application, going to a lower level of detail.

• It can be used in the refactoring process, to see the impact of a change, and to keep dependencies to a minimum, in order to have a project that is easier to understand and maintain.

• Integrating NDepend in the software development process. IMHO this is a key point, as using NDepend regulary will certainly improve the quality of the software that is being developed.  NDepend can generate a report of a project. A sample report is available here:

There are a lot of sections in the report, but I’ll concentrate on the section called “CQL Queries and Constraints”. That section is really important as it shows a list of items that can be problematic or don’t follow the standards of your company (methods with a lot of lines of code, very complex methods, poorly commented methods, types or methods or properties or fields with incorrect naming conventions, etc). This way the project leader can make periodic checks of those problems and report them to the people that are responsible for them.
This also allows you to know more the people you’re working with. Some developers don’t comment code, others like to make very long methods, and others tend to write complex logic that is difficult to understand by others. With the review of the report, they can be instructed so the resulting code and comments is better and more uniform.

However, there are times where a method with more than 30 lines of code is necessary. Or a method with a cyclomatic complexity of 25 is needed. So it is up to the project leader to review the suspicious method and inform the developers of the problem or simply acknowledge that it is necessary.

Unfortunately, NDepend does not provide built in support for this level of detail of project management. I have been told that in a future version it will provide hooks so custom scenarios like the exposed above are fully supported.

So, after what I have said about NDepend, you can be thinking, is it for me? Well, that depends on a lot of factors. I know a lot of companies that have no interest in achieving high quality products and that they only care about having a project done as soon as possible. If the maintenance of that project is a nightmare, well, that is another problem for another moment. However, there are some companies that actually spend time and effort to produce excellent products, so they may be interested in what NDepend can add for them.

I have used NDepend in several projects that are widely used when learning about it, and I found something curious:

The image shows that almost all of the NHibernate code has a big dependency cycle. I wish them good luck making the transition to ASTs.

To finish this post, I have to say that I have received a free professional version of NDepend. This has not conditioned my post about it. If I think it was a useless product I will not have posted about it.

Friday, February 8, 2008 1:22:13 PM (Romance Standard Time, UTC+01:00)  #    Comments [2]   ASP.NET | Microsoft .NET Framework | NHibernate  | 
Tuesday, July 29, 2008 6:24:09 AM (Romance Daylight Time, UTC+02:00)

I'm sure that the AST HQL parser don't create mayor problem basically because:
- the query translator is injectable
- NHibernate.LINQ exist (and it work with expression tree)
- we have some experiment with Gold parser
- we have same dependency than H3.2 and H3.2 work with AST HQL parser

BTW thank for "good luck".
Fabio Maulo
Tuesday, July 29, 2008 9:15:59 AM (Romance Daylight Time, UTC+02:00)

It is nice to hear that it won't be very problematic to add AST support. However, regarding your comment, I don't know if the AST support you mention is just to convert LINQ expressions to HQL or if it will be a deeper change that will make NHibernate to create the final SQL code using ASTs and not just string concatenation.

If it is the last case I thought it was a deeper change so I thought that the big dependency cycle shown in NDepend could cause problems (something that seemed odd as I consider the NHibernate code is well structurated). If it is easy to add, this shows that a metric always has to be taken with a grain of salt!

If the AST change you mention is the last case, my understanding is that will be able to fix some limitations of NHibernate like this:

Imagine a MyOrder that has a collection of MyOrderItem. MyOrder and MyOrderItem are entities.

Imagine that all orders have 3 order items and I have the join mode in the mapping file to retrieve the order items with an order. With the following code:

ISession session = SessionFactory.OpenSession();
ICriteria criteria = session.CreateCriteria(typeof(MyOrder));
criteria.AddOrder(new Order("Id", true));
IList<MyOrder> orders = criteria.List<MyOrder>();

what NHiberante does is a left outer join of the order and orderitems table, returning the first 10 rows of that join. That means that NHibernate returns only 4 orders, the first three with 3 order items each one, and the last one only with one order item.

There are other ORM that support this, so hopefully NHibernate will be able to support this.

Could you shed some light about it?

BTW, thank you very much for your work in NHibernate, I'm using the latest trunk for a month and it is getting better and better...
All comments require the approval of the site owner before being displayed.
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview
Copyright © 2021 Manuel Abadia. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.