假设我在我的解中设置了特定的方法。我怎样才能得到方法集中每个方法的平均代码行数?
这些数字通常显示在每个NDepend报告的统计部分(如Sum, Average, Minimum
等),但我希望能够为这些数字单独编写查询。
CQLinq查询可以如下所示:
let totalLinesSum = JustMyCode.Methods.Where(t => t.IsPublic).Sum(t => t.NbLinesOfCode)
let methodsCount = JustMyCode.Methods.Where(t => t.IsPublic).Count()
let result = (double)totalLinesSum / methodsCount
select (double?)result
…或者更精细一点,这个查询可以像这样重构:
// Let define your methods set the way you need
// It is worth removing abstract method that have no LoC
let methodsSet = JustMyCode.Methods.Where(m => m.IsPublic && !m.IsAbstract)
let totalLoc = methodsSet.Sum(t => t.NbLinesOfCode)
let methodsCount = methodsSet.Count()
let avgLoc = (double)totalLoc / methodsCount
select (double?)avgLoc