Max Params
ID |
swift.max_params |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Complexity |
Language |
Swift |
Tags |
complexity |
Description
Reports a function, initializer or subscript declaration whose parameter count exceeds a configurable threshold.
Rationale
High parameter counts make a function harder to call correctly, harder to remember the order of arguments at the call site, and almost always indicate the function has accumulated unrelated responsibilities. Swift mitigates some of this with argument labels, but the cognitive load still grows: each additional parameter is one more name the caller has to know and one more value the body has to coordinate.
Notes
Closures (function literals) are intentionally excluded: their parameter counts are usually dictated by the receiver (e.g. a UIKit completion handler), not the closure body, so penalising them does not lead to a useful refactor.
Remediation
Common refactors:
-
group related parameters into a small
struct(options object); -
split the function along its independent responsibilities;
-
default arguments to make rarely-used parameters optional at the call site.
// Too many parameters.
func process(a: Int, b: Int, c: Int, d: Int,
e: Int, f: Int, g: Int, h: Int) { ... }
// Group related values into a configuration struct.
struct Options { let a, b, c, d, e, f, g, h: Int }
func process(_ opts: Options) { ... }