Choosing Between Object Arguments and Argument Arrays in Your Functions

April 7, 2025 | 3 min read

When designing functions, one key decision is how to pass parameters: should you use object arguments or argument arrays (positional arguments)? This decision can greatly affect readability, maintainability, and flexibility of your codebase. Let's explore the pros, cons, and best use cases for each.

Readability and Intent

Object Arguments

Object arguments make function calls self-documenting. They're especially helpful when a function has multiple parameters, especially of the same type.

configureServer({ port: 8080, ssl: true });

Here, it's immediately clear what each argument represents. Inside the function, you typically destructure the object:

function configureServer({ port, ssl }: { port: number, ssl: boolean }) { // ... }

Positional Arguments

Positional arguments are concise and fine when the number of parameters is low:

createUser("Tyler", "Admin");

However, they can quickly become unreadable:

createUser("Admin", "Tyler"); // wait, which is the name?

Flexibility and Extensibility

Object Arguments

Using an object makes your function easier to extend over time. You can add optional parameters without breaking existing code:

function sendEmail({ to, subject, body, cc }: EmailOptions) { // ... }

Adding a bcc field later is trivial.

Positional Arguments

Adding new parameters requires modifying every call site or relying on awkward overloading:

function sendEmail(to: string, subject: string, body: string, cc?: string) { // ... }

This quickly gets messy.

Type Safety and Developer Experience

With TypeScript or similar tools, object arguments provide better autocomplete and inline documentation:

type CreatePostOptions = { title: string; content: string; authorId: number; tags?: string[]; };

This helps enforce clear expectations and avoid bugs.

API Design Philosophy

Object arguments align with named parameters in other languages (like Python or Swift). They provide a clear contract for the function.

On the other hand, positional arguments shine when the values have an obvious order:

calculateDistance(4, 6); // x, y

When to Use Each

SituationPrefer
More than 2–3 parametersObject arguments
Parameters are frequently optionalObject arguments
Parameters need clear labelingObject arguments
Function is internal & lightweightPositional
Parameters are naturally orderedPositional

Bonus: Hybrid Approach

Sometimes, a hybrid makes sense:

function scheduleTask(id: string, options: { delay?: number; retries?: number }) { // ... }

This keeps the main argument required and clean while maintaining flexibility for optional configurations.

Final Thoughts

Choosing between object and positional arguments isn’t just a matter of style — it affects how understandable, scalable, and safe your code is. For most non-trivial functions, object arguments tend to win out in the long run.

The next time you write a function, take a moment to ask: how many parameters might this grow to? Will I remember the order a month from now? If the answer makes you pause, reach for an object.

Copyright © 2025 | All rights reserved.

Built with: Next.js v15.1.3, Typescript v5, Tailwindcss v3.4.1, Directus v18.0.3