Functions with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their
position.
void setCoordinates(Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2) { // 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
void setOrigin(Integer x, Integer y, Integer z) {
// ...
}
void setSize(Integer width, Integer height, Integer depth) {
// ...
}
- Find a better data structure for the parameters that group data in a way that makes sense for the specific application domain
public class Point { // In geometry, Point is a logical structure to group data
Integer x;
Integer y;
Integer z;
}
void setCoordinates(Point p1, Point p2) {
// ...
}
This rule raises an issue when a function has more parameters than the provided threshold.