Script Valley
Writing Clean Code: Naming, Functions & Structure
Writing Functions That Do One ThingLesson 2.3

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

Function parameter count guide

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 thrown

When 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.

Up next

Side effects in functions and why they cause bugs

Sign in to track progress

How many parameters should a function take โ€” Writing Functions That Do One Thing โ€” Writing Clean Code: Naming, Functions & Structure โ€” Script Valley โ€” Script Valley