Avoid methods with too many parameters
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).
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+).
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>
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