Avoid methods potentially poorly commented
Matches methods with less than 10% comment lines and at least 20 lines of code; they might need more comments. Only comments in the method body are counted. Comments metrics: https://www.ndepend.com/docs/code-metrics#PercentageComment https://www.ndepend.com/docs/code-metrics#NbLinesOfComment
How to fix
Typically add comments — though clean code with well-named variables and methods, plus unit-tests, needs few comments. However hacks (non-trivial code circumventing API limitations or bugs) must absolutely be commented: intention, need, and source of the solution.
Estimated Debt: linear from 2 minutes (10 uncommented loc) to 20 minutes (200+).
Estimated Debt: linear from 2 minutes (10 uncommented loc) to 20 minutes (200+).
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 methods potentially poorly commented</Name>
// <Id>ND1006:AvoidMethodsPotentiallyPoorlyCommented</Id>
warnif count > 0 from t in JustMyCode.Types where
// Entity Framework ModelSnapshot and DbContext and Migration have large uncommented methods.
!t.DeriveFrom("Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot".AllowNoMatch()) &&
!t.DeriveFrom("Microsoft.EntityFrameworkCore.DbContext".AllowNoMatch()) &&
!t.DeriveFrom("Microsoft.EntityFrameworkCore.Migrations.Migration".AllowNoMatch())
from m in t.Methods where
m.PercentageComment <= 5 &&
m.NbLinesOfCode >= 30 &&
m.CyclomaticComplexity >=3 &&
JustMyCode.Contains(t)
let nbLinesOfCodeNotCommented = m.NbLinesOfCode - m.NbLinesOfComment
orderby nbLinesOfCodeNotCommented descending
select new {
m,
m.PercentageComment,
m.NbLinesOfCode,
m.NbLinesOfComment,
nbLinesOfCodeNotCommented,
Debt = nbLinesOfCodeNotCommented .Linear(20, 2, 200, 20).ToMinutes().ToDebt(),
// The annual interest varies linearly from interest for severity major for 300 loc
// to interest for severity critical for 2000 loc
AnnualInterest = m.PercentageComment.Linear(
0, 8 *(Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes),
20, Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes).ToMinutes().ToAnnualInterest()
}
//<Expl>The *{0}* is sufficiently complex, consider adding more comments for clarity.</Expl>
//<Desc>
// Matches methods with less than 10% comment lines and at least 20 lines of
// code; they might need more comments. Only comments in the method body are
// counted. *Comments metrics*:
// https://www.ndepend.com/docs/code-metrics#PercentageComment
// https://www.ndepend.com/docs/code-metrics#NbLinesOfComment
//</Desc>
//<HowToFix>
// Typically add comments — though clean code with well-named variables and
// methods, plus unit-tests, needs few comments. However **hacks** (non-trivial
// code circumventing API limitations or bugs) must absolutely be commented:
// intention, need, and source of the solution.
//
// Estimated Debt: linear from 2 minutes (10 uncommented loc) to 20 minutes
// (200+).
//</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