Avoid making large methods even larger
Runs only in diff mode (baseline defined). Warns when an already large method (more than 15 lines of code) became even larger since baseline. Metric: https://www.ndepend.com/docs/code-metrics#NbLinesOfCode
Requires PDB files at analysis time. See: https://www.ndepend.com/docs/ndepend-analysis-inputs-explanation
To visualize changes, right-click a matched method and select:
- Compare older and newer versions of source file
- or Compare older and newer versions decompiled with Reflector
Requires PDB files at analysis time. See: https://www.ndepend.com/docs/ndepend-analysis-inputs-explanation
To visualize changes, right-click a matched method and select:
- Compare older and newer versions of source file
- or Compare older and newer versions decompiled with Reflector
How to fix
Usually split too big methods into smaller ones. But long branch-free methods (typically data initialization) are not necessarily a problem.
Estimated Debt: linear from 10 to 60 minutes with the lines of code added. Severity is High: fix these issues now, before such code gets released in production.
Estimated Debt: linear from 10 to 60 minutes with the lines of code added. Severity is High: fix these issues now, before such code gets released in production.
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 making large methods even larger</Name>
// <Id>ND1105:AvoidMakingLargeMethodsEvenLarger</Id>
warnif count > 0
from m in JustMyCode.Methods where
!m.IsAbstract &&
// Eliminate constructors from match, since they get larger
// as soon as some field initializations are added.
!m.IsConstructor &&
!m.IsClassConstructor &&
// Filter just here for optimization
m.NbLinesOfCode > 25 &&
m.IsPresentInBothBuilds() &&
m.CodeWasChanged()
let oldLoc = m.OlderVersion().NbLinesOfCode
where oldLoc > 25 && m.NbLinesOfCode > oldLoc
let diff = m.NbLinesOfCode - oldLoc
where diff > 0
orderby diff descending
select new {
m,
oldLoc,
newLoc = m.NbLinesOfCode,
diff,
Debt = diff.Linear(1, 10, 100, 60).ToMinutes().ToDebt(),
// The annual interest varies linearly from interest for severity Medium for a tiny complexity increment
// to interest for severity critical for 2000 loc
AnnualInterest = diff .Linear(1, Severity.High.AnnualInterestThreshold().Value.TotalMinutes,
100, 4*(Severity.High.AnnualInterestThreshold().Value.TotalMinutes)).ToMinutes().ToAnnualInterest()
}
//<Expl>
// The *{0}* was already large in the baseline, comprising {1} statements,
// and has now expanded further, encompassing {2} statements.
// The implementation of a method should be concise enough to fit comfortably
// within the vertical screen space, ensuring readability.
// </Expl>
//<Desc>
// Runs only in *diff mode* (baseline defined). Warns when an already large
// method (more than 15 lines of code) became even larger since baseline.
// Metric: https://www.ndepend.com/docs/code-metrics#NbLinesOfCode
//
// Requires PDB files at analysis time. See:
// https://www.ndepend.com/docs/ndepend-analysis-inputs-explanation
//
// To visualize changes, right-click a matched method and select:
//
// - Compare older and newer versions of source file
//
// - or Compare older and newer versions decompiled with Reflector
//</Desc>
//<HowToFix>
// Usually split too big methods into smaller ones. But long branch-free
// methods (typically data initialization) are not necessarily a problem.
//
// Estimated Debt: linear from 10 to 60 minutes with the lines of code added.
// Severity is **High**: fix these issues **now**, before such code gets
// released in production.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Code Smells Regression: all 9 items
- From now, all types added should respect basic quality principles ND1100
- From now, all types added should be 100% covered by tests ND1101
- From now, all methods added should respect basic quality principles ND1102
- Avoid decreasing code coverage by tests of types ND1103
- Avoid making complex methods even more complex ND1104
- Avoid making large methods even larger ND1105
- Avoid adding methods to a type that already had many methods ND1106
- Avoid adding instance fields to a type that already had many instance fields ND1107
- Avoid transforming an immutable type into a mutable one ND1108