Functions with a long parameter list are difficult to use, as maintainers must figure out the role of each parameter and keep track of their
position.
fun setCoordinates(x1: Int, y1: Int, z1: Int, x2: Int, y2: Int, z2: Int) { // Noncompliant
// ...
}
The solution can be to:
- Split the function into smaller ones
// Each function does a part of what the original setCoordinates function was doing, so confusion risks are lower
fun setOrigin(x: Int, y: Int, z: Int) {
// ...
}
fun setSize(width: Int, height: Int, depth: Int) {
// ...
}
- Find a better data structure for the parameters that group data in a way that makes sense for the specific application domain
data class Point(val x: Int, val y: Int, val z: Int) // In geometry, Point is a logical structure to group data
fun setCoordinates(p1: Point, p2: Point) {
// ...
}
This rule raises an issue when a function has more parameters than the provided threshold.
Exceptions
Methods annotated with Spring’s @RequestMapping
(and related shortcut annotations, like @GetRequest
) or
@JsonCreator
may have a lot of parameters, encapsulation being possible. Therefore the rule ignores such methods.