Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view Code Metrics lke Fan-In/Fan-Out with NDepend

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.

like image 234
VincentZHANG Avatar asked Oct 29 '25 08:10

VincentZHANG


1 Answers

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

like image 101
Patrick from NDepend team Avatar answered Oct 31 '25 21:10

Patrick from NDepend team