Avoid methods potentially poorly commented

Rule ND1006 Code Smells

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+).

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>

Code Smells: all 10 items