4

I have installed NDepend (14-day trial version) as a Visual Studio 2015 Extension, and it works now.

I would like to get some metrics of some classes in my solution:

  • length of identifiers
  • fan in / fan out
  • weighted method of class
  • coupling of class objects

I did not find any useful instruction from its official site, does anyone know?

Thanks.

VincentZHANG
  • 757
  • 1
  • 13
  • 31

1 Answers1

6

You can write C# LINQ code queries to obtain pretty much any code metric you need.

length of identifiers

from t in Application.Types
select new { t, t.SimpleName.Length }

fan in / fan out

from t in Application.Types
select new { t, t.TypesUsed, t.TypesUsingMe }

weighted method of class

from t in Application.Types
select new { t, t.CyclomaticComplexity }

coupling of class objects (according to this definition)

from n in Application.Namespaces
let NumberOfClasses = n.ChildTypes.Count()
let NumberOfLinks = n.ChildTypes.SelectMany(t => t.TypesUsed).Distinct().Count()
select new { n, CBO = NumberOfLinks / (float)NumberOfClasses  }

You can then transform a code query into a code rule with the prefix warnif count > 0 and save the rule to get it executed in Visual Studio and/or in your BuildProcess.

// <Name>Type name shouldn't exceed 25 char</Name>
warnif count > 0
from t in Application.Types
where t.SimpleName.Length > 25
orderby t.SimpleName.Length descending
select new { t, t.SimpleName.Length }

enter image description here

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • However, I am still exploring NDepend, could you take a look at this question:http://stackoverflow.com/questions/37083906/how-to-use-cqlinq-to-get-metrics-of-methods-and-fields-within-a-single-query, it's about the same thing, but I think I had better discuss it in a separate question, thanks. – VincentZHANG May 08 '16 at 02:01
  • In addition, what is the algorithm of Cyclomatic Complexity for methods? Is it the same as described here: http://staff.unak.is/andy/StaticAnalysis0809/metrics/cyclomatic_complexity.html ? I found discrepancy. – VincentZHANG May 08 '16 at 05:53
  • Find all details about NDepend CC here: http://www.ndepend.com/docs/code-metrics#CC – Patrick from NDepend team May 09 '16 at 13:23