Search code examples
visual-studioadd-incode-metrics

Is there any tool that signals bad code metrics while writing code in Visual Studio?


Are there any tools available for Visual Studio that can inform the programmer about the code metrics status on the fly, e.g. while writing the code (in the form of a traffic light for instance)?


Solution

  • Try the tool NDepend. It is integrated in VS 2012, 2010, 2008 and comes with 82 code metrics listed here: http://www.ndepend.com/Metrics.aspx

    The tool comes with a set of around 200 predefined (and customizable) code rules defined over LINQ queries, some of them based on these code metrics, like for example:

    // <Name>Methods too complex</Name>
    warnif count > 0 from m in JustMyCode.Methods where 
      m.CyclomaticComplexity > 20 ||
      m.ILCyclomaticComplexity > 40 ||
      m.ILNestingDepth > 5
      orderby m.CyclomaticComplexity descending,
              m.ILCyclomaticComplexity descending,
              m.ILNestingDepth descending
    select new { m, m.CyclomaticComplexity, 
                    m.ILCyclomaticComplexity,
                    m.ILNestingDepth  }
    

    The tool can be tuned to make it so that rules are checked each time the developer re-compile a project or the solution. And indeed, a red/yellow/green traffic light is used to indicates if some rules are violated (rule violated => yellow, critical rules violated => red):

    enter image description here