Tuesday, 29 March 2011

Code, Quality, and NDepend

I got the idea of Quality with a capital “Q” ingrained upon me years ago when I read the philosophical novel Zen and the Art of Motorcycle Maintenance.  The main thing I took away from the book is that whatever human endeavor you are engaged in, you should let your actions be governed by a desire to do things well, with a constant and abiding interest in achieving the highest possible Quality.  Naturally this idea can (and should) be applied to the complex world of software development, particularly with respect to the question of Code Quality. 

It’s not always easy to live this way.  Whatever point we happen to be at in our lives or upon our career path, our knowledge only goes so far.  And sometimes we take shortcuts; it’s often tempting to take the easy (or lazy) way out, thinking we don’t have time to do things well or to do them right.

However, in this day and age, .Net developers are fortunate to have a plethora of 3rd party tools available to us to help us easily examine and improve the quality of our code.

Along those lines, I am appreciative I was asked by C# MVP Patrick Smacchia to take a look at his tool NDepend which extensively analyzes your .Net code to look for flaws based on rules you can configure, in the interest of improving Code Quality.  I ran it against a small MVC2 project I’m working on and it’s fascinating what I’ve learned just from a cursory examination.

I will say at this point I’m only scratching the surface of what this tool can do, but I can say even at this early stage that I would highly recommend it as a valuable addition to any .Net development toolbox, and I’m finding NDepend is teaching me things about my code and about .Net development in general that make me want to use and explore it a lot further.

When you install NDepend you have the option to install it as a Visual Studio add-in, which I did.  That enables an NDepend menu within VS:


I chose the option “Attach new NDepend Project…” and then chose “Run Analysis.”  The following dialog is displayed:


Now, you can choose one of the options shown here or select “Skip”, but by default, NDepend runs and displays the results of its analysis in your browser; a portion of the page is shown here:


In addition to the metrics and configurable CQL rules (Code Query Language), right off the bat you get some cool and interesting diagrams like the “Dependency Graph”:


You also get things like a “Summary of CQL Rules Violated”:


What if there are too many flaws reported?  I clicked on the “Code Quality from Now” link and got this dialog:


What if I don’t like the default set of CQL rules that NDepend gives me, or I want to change the rules to better suit my own needs?  I can choose the “CQL Query Explorer” from the NDepend menu:


The CQL Query Explorer looks like this:


Double-clicking on an item in the list, for example the “Methods too big (NbILInstructions)” item, brings up the CQL Query Editor:


It may be difficult to look at the flaws in your code, but, let’s face it:  it takes a strong person to admit that their method is too big.  ;-)

All kidding aside, I think you get the idea… the CQL language has a very intuitive SQL-like syntax, and you could decide you’d prefer a different value for the number of IL instructions, etc. 

Once again this is just the tip of the iceberg, but clearly NDepend is a great tool created by some very bright people.  I’m looking forward to mastering it more fully.

No comments:

Post a Comment