Use Few Function Arguments
Fewer arguments make your function easier to test. They are also easier to handle.
Usage
π Guideline
Use Few Function Arguments: Reduce the number of function arguments to enhance testability and simplify handling.
Functions with many arguments hinder understanding and reasoning. Testing complexity increases due to the need for values in multiple test cases. Minimize arguments for clarity and easier testing.
π οΈ How to apply
- Break down complex functions: If a function has too many arguments, consider breaking it down into smaller functions with fewer arguments. π§©
- **Group related arguments**: Identify arguments that are closely related and bundle them together into a single object or data structure. π¦
- Use default values: Utilize default values for optional arguments to minimize the need for passing multiple arguments. π·οΈ
- Less than 3 arguments: Aim for zero or few arguments, avoiding three or more to prevent excessive complexity. 3οΈβ£
Pros and Cons
π Pros
- Enhanced readability: Functions with fewer arguments are easier to understand and reason about. They have a clear purpose, making the code more readable. π
- Improved maintainability: When functions have fewer arguments, it's simpler to modify or extend them without cascading changes to other parts of the codebase. π§°
- Reduced coupling: Fewer arguments reduce the dependencies between functions, promoting loose coupling and better modular design. π
π Cons
- Increased dependency on shared state: When reducing function arguments, it's common to rely more on shared state or global variables. This can introduce hidden dependencies and make the code harder to reason about or debug. π΅οΈ
- Potential performance impact: In some cases, combining arguments or using configuration objects may introduce a slight performance overhead due to additional object creation or property access. π
Examples
β Bad
// Bad: Function with excessive arguments
function calculateTotalPrice(itemPrice: number, taxRate: number, discount: number, shippingFee: number): number {
// Complex logic involving all arguments
// ...
return totalPrice;
}
// Bad: Function with excessive arguments
function calculateTotalPrice(itemPrice: number, taxRate: number, discount: number, shippingFee: number): number {
// Complex logic involving all arguments
// ...
return totalPrice;
}
β Good
// Good: Function with reduced arguments
function calculateTotalPrice(itemPrice: number, options: { taxRate: number, discount: number, shippingFee: number }): number {
const { taxRate, discount, shippingFee } = options;
// Simplified logic utilizing the options object
// ...
return totalPrice;
}
// Good: Function with reduced arguments
function calculateTotalPrice(itemPrice: number, options: { taxRate: number, discount: number, shippingFee: number }): number {
const { taxRate, discount, shippingFee } = options;
// Simplified logic utilizing the options object
// ...
return totalPrice;
}
References
π Related principles
- Avoid Long Function: Minimizing the number of function arguments relates to the principle of avoiding long and complex functions. π
- Single Responsibility Principle: A function with fewer arguments aligns with the principle of having a single responsibility. π―
- Default Parameter Values: Using default values in function arguments can help reduce the number of explicit arguments passed. π·οΈ
- Object-Oriented Design: Grouping arguments into a single object aligns with the principles of object-oriented design and encapsulation. π§±