ARC-16Recommended
Pass few arguments
Give a function at most three arguments, or write down why it needs more. Usually, pass one named object that groups them.
How to do it
Group arguments that travel together into a parameter object or DTO, such as ShippingAddress. In Python use a dataclass, in PHP a small class with named arguments, in Java a record. Replace boolean flags with two functions, because a flag means the function does two things. Constructors of DTOs, records and value objects are exempt, because they are the grouping. Give each group a named type, as Use typed objects instead of loose arrays says.
Examples
TypeScript example
// Bad
createOrder(customerId, street, city, postcode, country, true);
// Good
createOrder({ customerId, shippingAddress, giftWrap: true });
Python example
@dataclass(frozen=True)
class NewOrder:
customer_id: str
shipping_address: ShippingAddress
gift_wrap: bool = False
create_order(NewOrder(customer_id, shipping_address, gift_wrap=True))
PHP example
createOrder(new NewOrder(customerId: $customerId, shippingAddress: $address, giftWrap: true));
How to check
A linter flags every function with more than three arguments. Examples are ESLint max-params set to 3, PHPMD ExcessiveParameterList with a minimum of 4, or Pylint too-many-arguments with max-args=3. Pylint leaves self out of the count. The build passes only when each flagged function has a disable comment that gives its reason.
Sources
Related rules
- ARC-49 Use typed objects instead of loose arrays in Clean code and architecture
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/names-and-functions/few-arguments">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.