EasyJavaScriptTypeScript

Limit Function Arguments

Clean CodeFunctionsRefactoring

Functions with many parameters are hard to call correctly — which order do the args go?

// dirty — 5 positional arguments
function solve(firstName, lastName, age, email, role) {
  return { firstName, lastName, age, email, role };
}

Refactor so solve accepts a single options object instead of 5 positional arguments.

solve({ firstName: 'Alice', lastName: 'Smith', age: 30, email: 'a@b.com', role: 'admin' })

Sample tests

Test #1Empty values pass through unchanged
Input: [{"age":0,"role":"","email":"","lastName":"","firstName":""}]
Output: {"age":0,"role":"","email":"","lastName":"","firstName":""}
Test #2Returns the same object shape
Input: [{"age":30,"role":"admin","email":"a@b.com","lastName":"Smith","firstName":"Alice"}]
Output: {"age":30,"role":"admin","email":"a@b.com","lastName":"Smith","firstName":"Alice"}
Test #3Works for any valid input
Input: [{"age":25,"role":"user","email":"b@c.com","lastName":"Jones","firstName":"Bob"}]
Output: {"age":25,"role":"user","email":"b@c.com","lastName":"Jones","firstName":"Bob"}