Avoid unmaintainable code

Rule ND1009 Code Smells

Detects methods with a low maintainability index and high cyclomatic complexity: hard to understand, error prone and risky to modify. Metrics definitions:

https://www.ndepend.com/docs/code-metrics#MaintainabilityIndex

https://www.ndepend.com/docs/code-metrics#CC

https://www.ndepend.com/docs/code-metrics#HalsteadVolume

How to fix

Refactor the method by splitting it into smaller, more focused components.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Avoid unmaintainable code</Name>
// <Id>ND1009:AvoidUnmaintainableCode</Id>

warnif count > 0 
from m in JustMyCode.Methods
where m.NbLinesOfCode >= 30 && m.MaintainabilityIndex <= 35 &&
      m.CyclomaticComplexity != null && m.CyclomaticComplexity >= 20
orderby m.MaintainabilityIndex ascending
select new { 
   m, 
   m.MaintainabilityIndex,
   m.CyclomaticComplexity, 
   m.NbLinesOfCode,
   m.HalsteadVolume,
   Debt = 
       (m.HalsteadVolume.Linear(1000, 6,  10000, 60) +
        ((40 - m.MaintainabilityIndex)/5f).Linear(1, 6,  20, 60))
       .ToMinutes().ToDebt(),
   Severity = Severity.Medium
}

//<Expl>
// The *{0}* has a low maintainability index *{1}* and is overly 
// complex. Consider breaking it down into smaller, more focused methods.  
// Introducing new types where appropriate may also help improve clarity 
// and maintainability.
//</Expl>

//<Desc>
// Detects methods with a low maintainability index and high cyclomatic
// complexity: hard to understand, error prone and risky to modify.
// Metrics definitions:
//
// https://www.ndepend.com/docs/code-metrics#MaintainabilityIndex
//
// https://www.ndepend.com/docs/code-metrics#CC
//
// https://www.ndepend.com/docs/code-metrics#HalsteadVolume
//</Desc>

//<HowToFix>
// Refactor the method by splitting it into smaller, more focused components.
//</HowToFix>

Code Smells: all 10 items