max-parameters-for-function

Same as max_parameters_for_function in Pyramid Lint

Path
lint/complexity/max-parameters-for-function
Default
Off, opt in
Origin
Pyramid Lint
Rule
263 of 512

Flags callable declarations and function types with more than the configured number of parameters.

A long parameter list is hard to call correctly and usually signals that some arguments belong together in an object. The rule counts required, optional-positional, and named parameters on functions, local functions, closures, methods, constructors, operators, setters, and function types.

Options

max-parameters (integer, default: 5) — flag when the parameter count exceeds this.

Invalid

example.dartdart
// Bad: function with 6 parameters exceeds max (default: 5)
void create(String a, String b, int c, double d, bool e, List<String> f) {
  print(a);
  print(b);
  print(c);
  print(d);
  print(e);
  print(f);
}

// Bad: method with 7 parameters
class Example {
  void process(int p1, int p2, int p3, int p4, int p5, int p6, int p7) {
    print(p1);
    print(p2);
  }

  // Another method with 8 parameters
  String buildString(String a, String b, int c, double d, bool e, List<String> f, Map<String, int> g, Set<int> h) {
    return '$a-$b-$c-$d-$e-${f.length}-${g.length}-${h.length}';
  }
}

// Bad: top-level function with 6 parameters
void configure(int x, int y, int z, double scale, bool enabled, String name) {
  print('$x, $y, $z, $scale, $enabled, $name');
}

class Wide {
  Wide(int a, int b, int c, int d, int e, int f);

  Wide copyWith({int? a, int? b, int? c, int? d, int? e, int? f}) => this;
}

typedef WideCallback = void Function(int a, int b, int c, int d, int e, int f);

void nested() {
  void local(int a, int b, int c, int d, int e, int f) {}
  final callback = (int a, int b, int c, int d, int e, int f) {};
}

void accepts(void callback(int a, int b, int c, int d, int e, int f)) {}
Function has too many parameters (max 5).
1// Bad: function with 6 parameters exceeds max (default: 5)
2void create(String a, String b, int c, double d, bool e, List<String> f) {
Function has too many parameters (max 5).
12class Example {
13 void process(int p1, int p2, int p3, int p4, int p5, int p6, int p7) {
Function has too many parameters (max 5).
18 // Another method with 8 parameters
19 String buildString(String a, String b, int c, double d, bool e, List<String> f, Map<String, int> g, Set<int> h) {
Function has too many parameters (max 5).
24// Bad: top-level function with 6 parameters
25void configure(int x, int y, int z, double scale, bool enabled, String name) {
Function has too many parameters (max 5).
29class Wide {
30 Wide(int a, int b, int c, int d, int e, int f);
Function has too many parameters (max 5).
31
32 Wide copyWith({int? a, int? b, int? c, int? d, int? e, int? f}) => this;
Function has too many parameters (max 5).
34
35typedef WideCallback = void Function(int a, int b, int c, int d, int e, int f);
Function has too many parameters (max 5).
37void nested() {
38 void local(int a, int b, int c, int d, int e, int f) {}
Function has too many parameters (max 5).
38 void local(int a, int b, int c, int d, int e, int f) {}
39 final callback = (int a, int b, int c, int d, int e, int f) {};
Function has too many parameters (max 5).
41
42void accepts(void callback(int a, int b, int c, int d, int e, int f)) {}

Valid

example.dartdart
// Good: function with 3 parameters under limit
void create(String a, String b, int c) {
  print(a);
  print(b);
  print(c);
}

// Good: using a config object instead of many parameters
class Config {
  final String name;
  final int count;
  final double value;
  final bool flag;
  final List<String> items;
}

void configure(Config config) {
  print(config.name);
}

// Good: method with 5 parameters (at limit)
class Example {
  void process(int p1, int p2, int p3, int p4, int p5) {
    print(p1);
  }

  // Good: constructor with 4 parameters
  Example.simple(String name, int age, double height, bool active) {
  }

  // Good: single parameter
  void singleParam(String value) {
    print(value);
  }

  // Good: no parameters
  void noParams() {
    print('Called with no parameters');
  }
}

// Good: optional parameters within limit
void functionalApproach({String? name, int? age, double? score, bool? flag}) {
  print('$name, $age, $score, $flag');
}

class AtLimit {
  final int a, b, c, d, e;

  const AtLimit(this.a, this.b, this.c, this.d, this.e);

  AtLimit copyWith({int? a, int? b, int? c, int? d, int? e}) => this;
}

typedef AtLimitCallback = void Function(int a, int b, int c, int d, int e);

Configure max-parameters-for-function

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "max-parameters-for-function": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/max-parameters-for-function: <reason>