Excessive number of method arguments
Why is this an issue?
Methods with many parameters are difficult to use correctly and maintain:
Error-prone: Callers may pass arguments in the wrong order.
Hard to read: Long parameter lists obscure the method's purpose.
Difficult to extend: Adding new parameters breaks existing callers.
Code smell: Often indicates the method is doing too much.
Examples
Example of incorrect code:
public void createContact(String firstName, String lastName, String email,
String phone, String title, String department, Id accountId,
Boolean isActive, String mailingStreet, String mailingCity) {
// Too many parameters
}
Example of correct code using a wrapper class:
public class ContactRequest {
public String firstName;
public String lastName;
public String email;
public String phone;
public Id accountId;
// Additional fields...
}
public void createContact(ContactRequest request) {
Contact c = new Contact(
FirstName = request.firstName,
LastName = request.lastName,
Email = request.email
);
insert c;
}
How can I fix violations?
Use wrapper classes: Group related parameters into a single object.
Use the builder pattern: For complex object construction with many optional parameters.
Split the method: If the method needs many parameters, it may be doing too much.
Use method overloading: Provide simpler versions with fewer parameters and sensible defaults.
When should I disable this rule?
You may dismiss specific violations for methods that interface with external systems requiring specific parameter signatures.
Resources
