Art Of Programming

musings by Dmytrii Nagirniak

NDepend

NDepend is
a tool that simplifies managing a complex .NET code base. Architects and developers can analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and master evolution by comparing different versions of the code.
Very interesting tool with a lot of you can do this and that.

It has just helped me to solve one issue. In my project I should never use DateTime.Now and DateTime.Today because of testability issues.
So I’ve decided to check if I haven’t missed to use my time accessor instead of static DateTime.Now and DateTime.Today. And it turns out there is ONE entry of it that I have never mentioned (and it obviously hasn’t been included in my tests set).
I’ve done it spending no more than couple of minutes to figure out how to write CQL (the NDepend Code Query Language):
SELECT METHODS FROM ASSEMBLIES "MyCompany.MyProject.BusinessObjects" WHERE IsUsing "System.DateTime.get_Now()" OR IsUsing "System.DateTime.get_Today()"


Then I also decided to have a look I have no types that start with underscore. Yeah, I renamed some of them to that in case I’ll need. I’d better delete them.
Anyway, here’s the query for it:
SELECT TYPES FROM ASSEMBLIES "MyCompany.MyProject.WebGUI" WHERE NameLike "^_" 
And I spot that guy. This ant was living in the compiled assembly.

Then I couldn’t stop. I wanted to know which methods are using Eco3 framework (previously partially owned by Borland):
SELECT METHODS FROM ASSEMBLIES "MyCompany.MyProject.WebGUI" WHERE IsUsing "Borland.Eco.*"

And I got ONE (huh, that’s good because of I plan to migrate from Eco3 to Eco5).
Then I remembered that I have references to my old assembly (Common) everywhere and I will have to migrate from it as well, so the query is simple:
SELECT METHODS FROM ASSEMBLIES "MyCompany.MyProject.WebGUI" WHERE IsUsing "MyCompany.Common.EcoUtils"
and I got about 1629 references.
So now I know I have to consider to keep backward compatibility in my Common assembly.

And there are so many things you can do with NDepend! You can even get lost executing different queries against your project and sometimes be surprised.
Using simple NDepend’s Search I found that I have class with IL Cyclomatic Complexity equal to 317! Guess what kind of class it is?..
Correct, it’s called Utils and has tons of static helper methods. This animal must be refactored.

Next thing I wanted to count number of my BusinessObjects created by ECO Framework (actual pages) in my project. That’s simple:
SELECT TYPES FROM ASSEMBLIES "MyCompany.MyProject.BusinessObjects" WHERE Implement "Borland.Eco.ObjectImplementation.ILoopBack"
and the answer was 85.

And this is non-stop process when playing with your code and NDepend…

And the other nice thing is that I can create warnings for myself, for example, I want NDepend to notify me about using System.DateTime.Now. So I can create a warning and automatically get the notification:
WARN Count > 0
IN
SELECT METHODS FROM ASSEMBLIES "MyCompany.MyProject.BusinessObjects" WHERE IsUsing "System.DateTime.get_Now()" OR IsUsing "System.DateTime.get_Today()"
That is really useful. Abilities of NDepend are really awesome.
But before digging deeper don’t forget to consult the CQL Specification.

Comments