A large number of parameters on public APIs should be avoided.


The problem

In Detecting Argument Selection Defects by Rice et. al., the authors argue that methods should have 5 of fewer parameters (see section 7.1). APIs with larger than this number often lead to parameter mismatch bugs (e.g, calling create(firstName, lastName) instead of create(lastName, firstName)).

In Effective Java (Item 2), Bloch recommends “consider[ing] a builder [pattern] when faced with many [constructor] parameters”. You may consider encapsulating your parameters into a single @AutoValue object, which is created using an AutoValue Builder.


Suppress false positives by adding the suppression annotation @SuppressWarnings("TooManyParameters") to the enclosing element.