Avoid methods with too many parameters

Rule ND1004 Code Smells

Matches methods with 8 or more parameters: painful to call, possible performance cost. NbParameters metric: https://www.ndepend.com/docs/code-metrics#NbParameters

Overrides of third-party methods and constructors calling a base constructor with 8+ parameters are skipped (lower-level problem).

How to fix

Add properties/fields to the declaring type, or pass a dedicated arguments class/structure (e.g. System.Diagnostics.ProcessStartInfo passed to System.Diagnostics.Process.Start(ProcessStartInfo)).

Estimated Debt: linear from 1 hour (8 parameters) to 6 hours (40+).

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 with too many parameters</Name>
// <Id>ND1004:AvoidMethodsWithTooManyParameters</Id>
warnif count > 0 from m in JustMyCode.Methods where 
  m.NbParameters >= 8 &&

  // Don't match a method that overrides a third-party method with many parameters
  !m.OverriddensBase.Any(mo => mo.IsThirdParty) &&

  // Don't match a constructor that calls a base constructor with many parameters
  !(m.IsConstructor && m.MethodsCalled.Any(
     mc => mc.IsConstructor && 
           mc.NbParameters >= 8 && 
           m.ParentType.DeriveFrom(mc.ParentType))) &&

  // Don't match DllImport P/invoke methods with many parameters
  !m.HasAttribute("System.Runtime.InteropServices.DllImportAttribute".AllowNoMatch())

  orderby m.NbParameters descending
select new { 
   m, 
   m.NbParameters,
   Debt = m.NbParameters.Linear(8, 1,  40, 6).ToHours().ToDebt(),

   // The annual interest varies linearly from interest for severity Medium for 7 parameters
   // to interest for severity Critical for 40 parameters
   AnnualInterest = m.NbParameters.Linear(8,  Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes, 
                                          40, Severity.Critical.AnnualInterestThreshold().Value.TotalMinutes).ToMinutes().ToAnnualInterest()
}

//<Expl>
// The *{0}* has {1} parameters. This leads to confusion 
// at each call site and negatively impacts overall maintainability.
//</Expl>

//<Desc>
// Matches methods with 8 or more parameters: painful to call, possible
// performance cost. *NbParameters* metric:
// https://www.ndepend.com/docs/code-metrics#NbParameters
//
// Overrides of third-party methods and constructors calling a base
// constructor with 8+ parameters are skipped (lower-level problem).
//</Desc>

//<HowToFix>
// Add properties/fields to the declaring type, or pass a dedicated arguments
// class/structure (e.g. *System.Diagnostics.ProcessStartInfo* passed to
// *System.Diagnostics.Process.Start(ProcessStartInfo)*).
//
// Estimated Debt: linear from 1 hour (8 parameters) to 6 hours (40+).
//</HowToFix>

Code Smells: all 10 items