Avoid unmaintainable code
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
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>
Open in the interactive Rules Explorer · All default rules
Code Smells: all 10 items
- Avoid types too big ND1000
- Avoid types with too many methods ND1001
- Avoid types with too many fields ND1002
- Avoid methods too big, too complex ND1003
- Avoid methods with too many parameters ND1004
- Avoid methods with too many overloads ND1005
- Avoid methods potentially poorly commented ND1006
- Avoid types with poor cohesion ND1007
- Avoid methods with too many local variables ND1008
- Avoid unmaintainable code ND1009