Analyzing Your .NET Code with NDepend

NDepend is a static analyzer that simplifies the management of a complex .NET code base. Architects and developers can analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and compare different versions of the code. The result is better communication, improved quality, easier maintenance and faster development.  Sounds good, eh?

Full disclosure: I received a free pro version of the NDepend from Patrick Smacchia last week along with little encouragement to use the tool and buzz about it if I find it useful.  Patrick’s timing, in fact, could not have been better as just the evening before a colleague of mine was asking for code metric tool recommendations.  Hopefully the review represents me well, but I am thrilled with NDepend and recommend it highly to anyone wanting to get “more familiar” with their code.

As published by Patrick Smacchia

…something difficult in promoting a tool such as NDepend is to educate about what it can bring to your development shop in terms of agility. NDepend comes with a set of innovative features currently not supported by any other .NET tool. I like to think that what tools such as ReSharper or CodeRush are doing to your code at micro level (i.e methods’ body structuring), NDepend does it at macro level (i.e class, namespace, assembly structuring). Hence, as a developer I personally use both kind of tools to automatically control every aspects of the code base I am working on…

Being a big fan of ReSharper, I had high hopes for NDepend.  Knowing my co-worker could use some help finding a good code analyzer and having a free copy of the software dropped in my lap, I had the immediate incentive to dig into the NDepend tool kit…

How to Get Started

Since I wasn’t all too familiar with NDepend, I opted to first gather basic information about the tool and capabilities.  Here’s what I did:

1. I watched two online demos: Getting started and VisualNDepend basics

2. I reviewed three online tutorials: How do I analyze my .NET applications using NDepend?, What does the NDepend report tell me about my code? and I want to go further to have a better control over my code

3. I read a recent NDepend review posted by Andre Loker and previewed Patrick Smacchia’s Blog for his latest comments.

First Impressions

Before even opening the software it was very clear that NDepend was super powerful and, as another colleague of mine recently said, “it can be a mind blower.”  As I am very sensitive to informatiimageon overload, I proceeded simply and cautiously by basically following the steps found in the online demos.  The Visual NDepend 2.9.0 IDE is friendly and somewhat familiar as the start page could be compared to that of Visual Studio.  Here you may create/open projects or analyze/compare assemblies using the provided shortcuts.  Additionally, you are presented with quick links to the online demos and the installers for NDepend Visual Studio and Reflector add-ins. 

imageI opted to analyze a set of assemblies.  After selecting a half dozen assemblies managed within my current application, the assembly analysis is run and then, after a few moments, the results are presented within the VisualNDepend UI. 

Remember my earlier comment about being sensitive to information overload?  Well, if I wasn’t ready for it, the tool’s default display may have knocked me off my seat.  As you can see in the accompanying screen shot, there are many views and the UI is quite busy.  For the experienced user this is great as the numerous windows actually work nicely together.  For a new user, in my opinion, the elaborate UI may be inappropriate — possibly intimidating — especially if the new user really only wants to gather a simple metric like how many lines of code (LOC) make up a specific component.

With this said, the UI can be customized and all the information is very useful once you understand it.  Don’t be intimidated by NDepend even though the first impression can be a “mind blower.”

I clicked around the UI for about 30 minutes and I quickly got a good sense of its power.  Certainly the demos, tutorials and blogs noted above helped lessen my learning curve so I encourage you to follow my footsteps.

Code Query Language (CQL)

What impressed me most about the NDepend is its Code Query Language (CQL).  Per the NDepend site:

NDepend considers your code as a database and you can write some CQL statements to query and check some assertions on this database.

Out-of-the-box, NDepend comes with dozens of queries which fall into varying categories from Code Quality to Test Coverage .NET Framework Usage. 

And NDepend provides the same 82 code metrics to support you in building your own queries!   Here are a few simple examples of what you can do with the CQL:image

1. Which public methods have more than 30 lines of code?
SELECT METHODS WHERE NbLinesOfCode > 30 AND IsPublic

2. Which classes implement System.IDisposable?
SELECT TYPES WHERE IsClass AND Implements “System.IDisposable”

3. Which methods have been refactored recently and is not thoroughly covered by tests?
SELECT METHODS WHERE CodeWasChanged AND PercentageCoverage < 100

On top of that you can enable/disable or even edit the out-of-the-box queries.  For example, if you don’t agree that all static fields should be prefixed with an ‘s_’ change the query to validate your standard or remove the check all together.

With the Code Query Language, the sky is the limit.  Did I mention the editor comes with Intellisense?

Reports

Go ahead and generate a report.  Check out this example which gives you a run down of the application and assembly metrics, an assembly dependency diagram, CQL Queries and Constraints and much more.  Whereas the VisualNDepend UI is geared toward the architect or lead developer type who is wanting to really dig into an application, the report seems to be more suitable for a less technical audience.  Perhaps the report could be used as part of an executive summary or could complement a developer’s code review.  Simply, it is a professional output (HTML only, I think) consisting of endless information and even pictures. 

Integration

Very quickly I wanted to call out that NDepend integrates not only with VisualStudio and Reflector but also MSBuild, NAnt, and CruiseControl.NET.  I personally think it is great that NDepend was implemented in a manner in which its frequent (read: continuous) and easy (read: automated) use is promoted.  Well done.

Final Thoughts

NDepend is darn impressive. It is a “mind blower” if you will.  NDepend provides more than ample metrics, a flexible (albeit busy) UI, a customizable query language, professional reports and hooks into applications like Visual Studio and Reflector along with support for processes like automated builds and continuous integration.  If you really want to know your code and you are looking for a tool which provides more than simple application metrics, NDepend may be the right product for you.  I highly suggest you check it out.

Comments

  1. When installing Joomla on my computer in order to update a preexisting site, do I need my client to give me the Host Name, MySQL User Name, MySQL Password, MySQL Database NAme and MySQL Table Prefix? I already have their FTP information, and the Joomla admin control panel login information..

closed