Complex Methods should be 100% tested
Detects methods with a low maintainability index and insufficient test coverage: hard to understand, error prone and risky to modify. Maintainability index definition: https://www.ndepend.com/docs/code-metrics#MaintainabilityIndex
How to fix
1. Break large or complex methods into smaller, focused units.
2. Improve readability: clear naming, consistent formatting, less nesting.
3. Write unit tests validating both common and edge cases.
4. Ensure refactored code is fully covered by tests before merging.
2. Improve readability: clear naming, consistent formatting, less nesting.
3. Write unit tests validating both common and edge cases.
4. Ensure refactored code is fully covered by tests before merging.
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>Complex Methods should be 100% tested</Name>
// <Id>ND1610:ComplexMethodsShouldBeFullyTested</Id>
warnif count > 0
from m in JustMyCode.Methods
where m.NbLinesOfCode >= 30 && m.MaintainabilityIndex <= 40 &&
m.CoverageDataAvailable && m.PercentageCoverage < 100 &&
m.CyclomaticComplexity != null && m.CyclomaticComplexity >= 10
orderby m.MaintainabilityIndex ascending
select new {
m,
m.MaintainabilityIndex,
m.CyclomaticComplexity,
m.NbLinesOfCode,
m.PercentageCoverage,
Debt =
(((60 - m.MaintainabilityIndex)/25f)*(100 - m.PercentageCoverage))
.Linear(10, 6, 150, 120).ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The *{0}* has a low maintainability index *{1}* and is highly complex.
// Current test coverage is only *{4}%*.
// Refactor the code and add sufficient tests to ensure full coverage
// and improved maintainability.
//</Expl>
//<Desc>
// Detects methods with a low maintainability index and insufficient test
// coverage: hard to understand, error prone and risky to modify.
// Maintainability index definition:
// https://www.ndepend.com/docs/code-metrics#MaintainabilityIndex
//</Desc>
//<HowToFix>
// 1. Break large or complex methods into smaller, focused units.
//
// 2. Improve readability: clear naming, consistent formatting, less nesting.
//
// 3. Write unit tests validating both common and edge cases.
//
// 4. Ensure refactored code is fully covered by tests before merging.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Code Coverage: all 15 items
- Code should be tested ND1600
- New Types and Methods should be tested ND1601
- Methods refactored should be tested ND1602
- Assemblies and Namespaces should be tested ND1603
- Types almost 100% tested should be 100% tested ND1604
- Namespaces almost 100% tested should be 100% tested ND1605
- Types that used to be 100% covered by tests should still be 100% covered ND1606
- Types tagged with FullCoveredAttribute should be 100% covered ND1607
- Types 100% covered should be tagged with FullCoveredAttribute ND1608
- Methods should have a low C.R.A.P score ND1609
- Complex Methods should be 100% tested ND1610
- Test methods should assert something ND1611
- Test Methods
- Methods directly called by test Methods
- Methods directly and indirectly called by test Methods