
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
How we design function parameters has a big impact on code readability, flexibility, and maintainability. A powerful pattern is passing an object as a function parameter, especially when a function takes multiple related values. Combined with ES6 object destructuring, this approach leads to cleaner and more robust code. It’s an approach seen used in React and now with ServiceNow’s ECMAScript 2021 (ES12) mode, you can use it in your next script.
Why Pass an Object Instead of Multiple Arguments?
Passing individual arguments works fine for simple functions, but as soon as a function requires three or more parameters—especially optional ones—things can get messy fast.
Consider this function signature:
function getApprover(name, position, title, department, isContractor, employment_type, approval_level) { ... }
For some context, this is a function I saw recently that fetches from SAP an approver within the employee’s reporting line at different levels. Think, your manager’s, manager and so on.. This is a gross oversimplification since in reality there are all sorts of factors such as contractors and different departments that change how the look up is done.
This function raises several issues:
- It's hard to tell what each argument represents when calling the function.
- Optional arguments force you to maintain a specific order or use null/undefined as placeholders.
- Adding new parameters requires changes to every call site.
By contrast, passing an object solves all of these problems:
function getApprover({name, position, title, department, isContractor = false, employment_type, approval_level = 1}) { ... }
Now, the function call becomes more expressive and flexible:
getApprover({
name: 'Alice',
position: 'Line Manager',
department: 'Shipping',
});
This approach offers several key benefits:
Improved Readability
Object keys act as labels for each value, making it obvious what each parameter means—both in the function signature and at the call site.
Flexibility with Order
Because objects are key-based, you don't have to worry about the order of parameters. This is especially helpful for optional or infrequently used values.
Easier to Extend
Need to add a new parameter? Just update the function and include it in the object. Existing calls won’t break, and you won’t need to rewrite every usage.
Destructuring Support
With ES12 mode enabled, destructuring in the parameter list lets you extract only the properties you need—and even assign default values—right where the function starts.
function logEvent({ message, level = 'info', timestamp = new Date() }) {
if (level == 'info')
gs.info(`${timestamp.toISOString()}: ${message}`);
}
Note: I usually use script includes to contain functions but while creating some quick examples for this article I used a Fix Script instead. I was surprised to find destructuring is not supported in the Fix Script. However, it works if the fix script calls the script include. So be wary that the ECMAScript 2021 (ES12) mode may not have the same level of support in all ServiceNow script fields.
In summary, passing objects as parameters is a best practice that boosts the clarity, flexibility, and scalability of your functions. When paired with destructuring, it leads to cleaner code that's easier to understand and evolve over time.
- 1,052 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.