DEV Community

miku86
miku86

Posted on

List of Variables vs. Object

Recently, I received a question on some JavaScript stuff:

im not sure how it helps passing an object instead of a list of variables…

Code with a more precise explanation

Before:

function createMessageWithListOfVars(firstName, lastName, age) {
  return `${firstName} ${lastName} is ${age} years old.`
}

console.log(createMessageWithListOfVars("Andreas", "Max", 30));
Enter fullscreen mode Exit fullscreen mode

After:

function createMessageWithObjectDestruct({ firstName, lastName, age }) {
  return `${firstName} ${lastName} is ${age} years old.`
}

console.log(createMessageWithObjectDestruct({
  firstName: "Andreas",
  age: 30,
  lastName: "Max"
}));
Enter fullscreen mode Exit fullscreen mode

Oldest comments (12)

Collapse
 
bakedbird profile image
Eleftherios Psitopoulos • Edited

As you mention in your explanation, clarity and no-mistake are the most important reasons.

Collapse
 
qm3ster profile image
Mihail Malo

This is what we get for not having named params.

Collapse
 
rajatkantinandi profile image
Rajat Kanti Nandi

Recently seen this tweet from Samantha Ming.
I think this might be the reason..
twitter.com/samantha_ming/status/1...

Collapse
 
tomekbuszewski profile image
Tomek Buszewski

Okay, and what's the conclusion? Which notation is better in your opinion? :)

I use object as a parameter when the number of variables is greater than two.

Collapse
 
miku86 profile image
miku86

Tomek, great question.

I use object as a parameter when the number of variables is greater than one. Especially for using async stuff I don't want to care about the correct order.
Most of the time I also add some default parameters into the function declaration.

Collapse
 
mahmoudzohdi profile image
Mahmoud Zohdi

yesterday, I was thinking about this style of code and I found it's a good approach in functions with a lot of params.
but you shouldn't use a lot of this to keep your code clean.

Collapse
 
miku86 profile image
miku86

Hey Mahmoud,

thanks for your comment.

What is your understanding of keep your code clean in this case?

Collapse
 
mahmoudzohdi profile image
Mahmoud Zohdi

I mean if you used an object with properties instead of multiple params in this case you can send extra properties without keep tracking of it.

I don't know if this a big deal or not but I think it's not good to use this approach in all your app

Collapse
 
hdennen profile image
Harry Dennen

In my experience using object notation makes it easier to make changes later, and with typescript updating an object interface helps catch any errors when making that change.

Also, if you're into functional composition then having one parameter is ideal.

Collapse
 
shayd16 profile image
Shayne Darren

While this is great for functional composition and keeping code clean, I think it should be used with caution. One of the reasons as to why having too many parameters in a method is bad is that the method might be doing too much work, just because we reduce to total parameter count doesn't mean we will fix the underlying issue. Instead, rewrite/split the method into several methods that take fewer arguments.

Collapse
 
miku86 profile image
miku86

Hey Shayne,

I absolutely agree with you.
Methods should do one thing and too much parameters are a good hint that a method perhaps could be broken up into smaller methods.

Collapse
 
akashkava profile image
Akash Kava

How would you make parameters optional?