NDepend is a tool that offers a wide range of features to let developers analyze a .NET code base. It comes with about 200 built in rules. But there are a few default rules that do not fit “my rules”. This blog post shows how to customize one and even more important why.

Quick summary of methods to refactor

This rule gives you a quick overview of what needs to be refactored. I adjusted the rule so that compiler generated delegates that have a high “IL Nesting Depth” do not appear.

Sample code for this case:

var tablesNames =
  from tableRow in conn.GetSchema("Tables").Rows.Cast<DataRow>()
  let tableName = tableRow["TABLE_NAME"].ToString()
  where !tableName.Contains("$")
  ...
  select tableName;
warnif count > 0 from m in JustMyCode.Methods where
  m.NbLinesOfCode > 50 ||
  (m.NbILInstructions > 200 && m.ILCyclomaticComplexity > 50 ) ||
  m.CyclomaticComplexity > 20 ||
  m.ILCyclomaticComplexity > 50 ||
  (m.ILNestingDepth > 5 && m.ILCyclomaticComplexity > 10) ||
  m.NbParameters > 5 ||
  m.NbVariables > 20 ||
  m.NbOverloads > 25

select new {
    m, m.NbLinesOfCode,
    m.NbILInstructions,
    m.CyclomaticComplexity,
    m.ILCyclomaticComplexity,
    m.ILNestingDepth,
    m.NbParameters,
    m.NbVariables,
    m.NbOverloads
  }