Too Many Parameters
ID |
java.max_params |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Complexity |
Language |
Java |
Tags |
complexity |
Description
Reports a method or constructor declaration whose parameter count is strictly greater than the configured limit (7 by default).
A method annotated @Override is never reported. Its signature is inherited from a supertype, so the author of the overriding method cannot shorten it; the base declaration is reported instead, which is where the fix belongs. Constructors have no such exemption because they override nothing.
Rationale
A long parameter list is hard to call correctly. Consecutive arguments of the same type are easy to transpose, and the compiler cannot catch it: book(origin, destination, …) and book(destination, origin, …) both compile. The call site also stops explaining itself — create(true, false, 3, null, "x", 0, 7, false) tells the reader nothing about what those values mean.
A member that needs that many inputs has usually accumulated several responsibilities, or is carrying a group of values that travel together everywhere and deserve a type of their own.
public ShipmentBooking(String origin, String destination, double weight, double volume,
boolean express, boolean insured, String currency, String notes) {
...
}
// at the call site, nothing says which boolean is which
new ShipmentBooking(from, to, 12.5, 0.8, true, false, "EUR", null);
Remediation
Group the parameters that belong together into a dedicated type and pass that instead, or split the member so each part takes only the inputs it needs. A builder is an alternative when many of the values are optional.
public ShipmentBooking(Route route, Load load, BookingOptions options) {
...
}
new ShipmentBooking(
new Route(from, to),
new Load(12.5, 0.8),
BookingOptions.express().currency("EUR"));