Number of Parameters (NOP)
Number of parameters is a quantitative metric that measures the number of parameters a function/method has. High number of parameters often indicates functions/methods that are doing a lot of things and are hard to test and maintain.
This metric is not calculated for methods that return the same instance as the enclosing class (e.g. copyWith).
To get real-time analysis for this metric, enable the avoid-long-parameter-list rule.
Why Be Cautious
High number of parameters can lead to:
- Difficult Usage: Functions with many parameters are harder to understand and use correctly.
- Maintainability Challenges: Updating a function with many parameters requires updating all its calls, which can be tedious and error-prone.
- Increased Complexity: Too many parameters make functions more complicated and harder to test.
How to Address High Number of Parameters?
First, review all parameters for potential simplifications. For example, some parameters might not be used (referenced) inside the body of the function/method. In other cases, if a function takes several fields from an existing object, passing the entire object can both make the function API clearer and reduce the number of parameters.
Alternatively, consider either splitting the function/method into several smaller ones or creating classes to wrap related parameters. Grouping parameters into dedicated classes also signals how a particular value is expected to be obtained. For example, when a function accepts String value, any field that is a String can be passed to that parameter. But when the function accepts a custom wrapper, e.g. SomeClass, only an instance of that class can be passed and if it cannot be instantiated anywhere, it also signals that there is a particular way to obtain that instance which in turn reduces the number of potential bugs.
Config Example
dcm:
metrics:
number-of-parameters:
threshold: 4
To set multiple threshold or other common config options, refer to the Configuring Metrics page.
Example
To view what contributes to the metric value, generate an HTML report.
❌ Bad: High Number of Parameters
// NOP: 6
void createOrder(
String customerName,
String customerAddress,
int orderId,
List<Item> items,
DateTime orderDate,
bool isExpressDelivery,
) {
// Function logic
}
✅ Good: Low Number of Parameters
// NOP: 1
void createOrder(OrderDetails details) {
// Function logic
}
class OrderDetails {
final String customerName;
final String customerAddress;
final int orderId;
final List<Item> items;
final DateTime orderDate;
final bool isExpressDelivery;
OrderDetails({
required this.customerName,
required this.customerAddress,
required this.orderId,
required this.items,
required this.orderDate,
required this.isExpressDelivery,
});
}