Patrick Smacchia [MVP C#]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • NDepend v3 is fully integrated in Visual Studio, and is now available for download! Software dependencies visualization, 82 .NET software metrics, continuous rule validations, assembly version diff, declarative code queries and more ! http://ndepend.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Composing Code Metrics Values

NDepend provides 82 different code metrics which are all explained on this page. Several software development topics are addressed by these metrics, like:

 

Metrics constitute one feature of NDepend amongst several others features like comparing code base snapshots, digging in the structure of a code base through visual artifact like matrix and graph, or defining active rules.

 

 

I would like to focus here on some tricks to get the most of NDepend code metrics. Sometime the value of a metric is an immediate indicator to detect code flaws. Methods with more than 6 parameters and classes with a depth of inheritance greater than 8 are obviously flaw.

 

But some other times, the value of a code metric might be meaningless outside of some contextual information. For example, a class 100% covered by tests is certainly a good thing. But this information is more relevant for a large class with 300 lines of code than for a mini-class with 10 lines of code.

 

The same for a large method with 100 lines of code, this is a bad thing! But if the method has no cyclomatic complexity (meaning if the method has no loop, if, else, switch, try/catch…) this is not such a big deal since the method might still be easy to understand and to maintain. On the other hand a small method with 10 lines of code can become a real nightmare to maintain, especially if it is writing some fields.

 

To handle properly such cases, we need to correlate different metrics and eventually some other properties of the code base. This is possible in NDepend thanks to the Code Query Language flexibility.

 

For example you might want to know which classes are more than 95% covered by tests but not 100 covered yet. To avoid noise, you might wish to focus on larger classes. With CQL you can use an ORDER BY clause to sort classes by their number of lines of code and a TOP clause to limit the number of match.

 

 

 

As you can see the query result automatically contains all metrics involved in the CQL query and it is up to you to append more metrics in the query. As a bonus you can see that for each metric displayed in the query result you'll get statistical information like average and standard deviation.

 



 

 

Another example. Classes that use many other types (in other words, classes that have a high Efferent Coupling, Ce) are likely problematic. Indeed a high Ce often means too many responsibilities for a class. Pinpointing classes with high Ce is easy.

 

 

 

But doing so, you will certainly match primarily UI classes, like forms and controls. Indeed, UI frameworks often foster classes with high Ce due to the need to have some kind of large mediator classes to makes the different UI components communicate. You might then want to discard forms and controls classes:

 

 

 

And this last screenshot underlines a cool feature of CQL: the depth of usage metrics. You can see that each DeriveFrom XXX condition leads to a DepthOfDeriveFrom XXX metric. Obviously, here the values of this metric are N/A since we especially ask for classes that are not deriving from XXX. But you can use this ability to build depth of metrics usage on the fly to know for example where are your UI forms and sort the list by their DepthOfDeriveFrom Form class.

 

 

 

The depth of usage trick is not limited to inheritance and you can use it on the object creation usage with the condition DepthOfCreateA, on the writing field usage with the condition DepthOfIsWritingField and more generally on any kind of usage with the condition DepthOfIsUsing and DepthOsIsUsedBy.

 

 

As I explained previously on this blog, the depth of usage trick has several interesting applications such as getting all paths from a code element A to another code element B

 

 

… or building some call graph or class diagram as explained here

 


 


Posted Tue, Nov 25 2008 7:44 PM by Patrick Smacchia

[Advertisement]

Comments

Dew Drop - November 26, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - November 26, 2008 | Alvin Ashcraft's Morning Dew
on Wed, Nov 26 2008 11:26 AM

Pingback from  Dew Drop - November 26, 2008 | Alvin Ashcraft's Morning Dew

active matrix tft | WoW Gold,World of Warcraft Gold wrote active matrix tft | WoW Gold,World of Warcraft Gold
on Wed, Nov 26 2008 12:08 PM

Pingback from  active matrix tft | WoW Gold,World of Warcraft Gold

tft active matrix | WoW Gold,World of Warcraft Gold wrote tft active matrix | WoW Gold,World of Warcraft Gold
on Wed, Nov 26 2008 1:28 PM

Pingback from  tft active matrix | WoW Gold,World of Warcraft Gold

active matrix tft | CNN.com wrote active matrix tft | CNN.com
on Fri, Nov 28 2008 8:57 PM

Pingback from  active matrix tft | CNN.com

tft active matrix | CNN.com wrote tft active matrix | CNN.com
on Fri, Nov 28 2008 9:22 PM

Pingback from  tft active matrix | CNN.com

Alejandra wrote re: Composing Code Metrics Values
on Fri, Apr 24 2009 3:00 PM

Hello, how can I create the maintainability index with cql?

Does the NDepend calculate this number?

Thanks,

Alejandra (Argentina, Buenos Aires).

athmoredur wrote re: Composing Code Metrics Values
on Mon, Jun 1 2009 1:06 AM

shop particular joint main <a href="http://lists.jboss.org">1960 fossil</a> [url=http://www.buzzmachine.com]rate model past kyoto gun[/url] www.datacenterdynamics.com

shepherdki wrote re: Composing Code Metrics Values
on Mon, Jun 1 2009 1:07 AM

details glacial stratosphere windows part colleagues

oldwinwhit wrote re: Composing Code Metrics Values
on Tue, Jun 9 2009 9:13 AM

sensitivity simulation 180 troposphere forward

whitlawkuh wrote re: Composing Code Metrics Values
on Mon, Jun 29 2009 2:20 PM

scenarios india likely air gun possibly natural new

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us