question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't use objects as arguments.

See original GitHub issue

Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. Anything more than that should be consolidated. Usually, if you have more than two arguments then your function is trying to do too much. In cases where it’s not, most of the time a higher-level object will suffice as an argument.

Since JavaScript allows us to make objects on the fly, without a lot of class boilerplate, you can use an object if you are finding yourself needing a lot of arguments.

Unless there are multiple optional arguments it does more harm than good to pass an object with arguments.

  1. Your IDE has no idea what arguments the function is expecting so hinting is a no go.
  2. The user has no idea what the options are without good documentation.
  3. func({foo: "bar", bar: "foo"}) looks worse than func("bar", "foo")

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
vsemozhetbytcommented, Jan 9, 2017
1reaction
ryanmcdermottcommented, Jan 10, 2017

I understand where you are coming from, might want to take this up with Uncle Bob 😉

Objects as arguments, as @vsemozhetbyt stated, are valid and a good practice in JavaScript. Where JavaScript lacks most strongly is in a type system, so it can seem like creating higher-order objects to pass into functions is a waste as you can’t even type-check what is passed in. It does have positive refactoring benefits later on though if you use objects. For example, you can change the behavior of the object’s getters and setters, and you can provide some boundaries around the primitive values you pass in.

Forgive me being pedantic, linking out to Clean Code helps state this better than I did: “Reducing the number of arguments by creating objects out of them may seem like cheating, but it’s not. When groups of variables are passed together, the way x and y are in the example above, they are likely part of a concept that deserves a name of its own.” (Pg. 43)

I’ll let this issue serve as the authoritative final thought on this objection. I’ve been in the same boat before, and many people are! I still regularly write 7-argument functions all the time and say “What the heck, it’s not even a cohesive argument structure anyway… oh wait!” If you have a lot of arguments and can’t group them in one object, your function is doing too much! Make it do one thing.

I will refactor the description of this, since so many people have questions!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do not use mutable objects as default arguments in Python
This only applies to mutable objects. If you are using, say, integers as the default values for your arguments, then there is no...
Read more >
Should we avoid custom objects as parameters?
Using a custom object to group related parameters is actually a recommended pattern. As a refactoring, it is called Introduce Parameter Object.
Read more >
Why not use Object for all method parameters? - java
Read this article about Autoboxing in java. Is there a specific reason why people don't always use "Object" as their data type to...
Read more >
Chapter 7. Object arguments: functions working with objects
Just as passing objects to functions as arguments is an efficient way of moving information to where it's needed, so is using objects...
Read more >
Do not use mutable objects as default arguments in Python
You can give a plain function a static variable by exploiting the fact that even a function itself is an object. def keep_a_total(n:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found