How many parameters should a function take
parameter count limits, parameter objects, niladic and monadic functions, flag arguments antipattern, builder pattern for arguments
Function Parameters: Fewer Is Better
Every parameter is a concept the caller must understand and a value they must supply. The more parameters a function takes, the harder it is to call, test, and understand.
Zero parameters (niladic) is best. One (monadic) is clean. Two (dyadic) is acceptable if the pairing is natural (like x, y coordinates). Three or more is a problem.
// Bad โ five parameters, caller must remember order
function createUser(name, email, age, role, isActive) { }
// Callers make mistakes like this:
createUser('admin', 'john@x.com', true, 25, 'user');
// Swapped age and isActive โ no error thrownWhen you hit three or more parameters, use a parameter object:
// Good โ named properties, order doesn't matter
function createUser({ name, email, age, role, isActive }) { }
createUser({
name: 'John',
email: 'john@x.com',
age: 25,
role: 'user',
isActive: true
});Flag arguments are always wrong. A boolean parameter that changes what a function does internally means the function secretly does two things.
// Bad โ what does false mean here?
renderUser(user, false);
// Good โ two honest functions
renderUserCard(user);
renderUserSummary(user);The parameter object pattern also makes refactoring safe โ you can add new optional properties without breaking existing callers.
