Sony Arouje

a programmer's log

CQLinq-NDepend’s Code Query Linq

leave a comment »

Recently I got the new version of NDepend. Thanks to Patrick Smacchia and team to build such a wonderful tool for the developers. 

In one of my previous post I explained the features of NDepend and then after NDepend become the most important tool to make my code healthy and maintainable. One of the NDepend feature really knocked me out is CQL, query the code using SQL like syntax. That was a new experience to me, in my last post I wrote a CQL to find the dead functions in my application as shown below.

SELECT  METHODS WHERE
MethodCa == 0 AND !IsPublic AND !IsEntryPoint AND  !IsExplicitInterfaceImpl
AND !IsClassConstructor AND !IsFinalizer

Isn’t it cool if we can write these kind of queries in Linq. Yes we can, with the latest version of NDepend V4 we can write Linq queries to query our code and is called CQLinq. Let’s see how we can rewrite the above query using CQLinq.

from  m in JustMyCode.Methods where m.MethodsCalled.Count() ==0 && m.IsPublic==false 
&& m.IsEntryPoint==false && m.IsExplicitInterfaceImpl==false &&
m.IsClassConstructor==false && m.IsFinalizer==false select m

 

How about the query writer supports intellisense and integratd tool tip documentation? Awesome… I love these guys. Previously I need to remember the keywords like IsEntryPoint/IsExplicitInterfaceImpl etc. Now intellisense will show all the methods and properties. Also tool tip help tells us what is the use of the selected method or property. Cool you don’t have to remember every thing, less work for brain 🙂

See the screen shot below.

 
image
 

If we want to add our own rules and the UI want to show the warning symbol if the query returns value then add warnif  as shown below.

// <Name>Dead Methods</Name>
warnif count > 0 from  m in JustMyCode.Methods where m.MethodsCalled.Count() ==0 && m.IsPublic==false 
&& m.IsEntryPoint==false && m.IsExplicitInterfaceImpl==false &&
m.IsClassConstructor==false  select m

 

The Query and Rules explorer window will show the warning as shown below

image

I just covered very basic of NDepend’s CQLinq. You can find more detailed feature list of NDepend V4 and example CQLinq code from NDepend

 

Written by Sony Arouje

September 29, 2012 at 12:17 am

Posted in .NET

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: