Don't use objects as arguments.
See original GitHub issueZero 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.
- Your IDE has no idea what arguments the function is expecting so hinting is a no go.
- The user has no idea what the options are without good documentation.
func({foo: "bar", bar: "foo"})
looks worse thanfunc("bar", "foo")
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Some info to consider:
Object arguments are de facto standard. For example, they are used in many Node.js API functions .
This method is advised by some authoritative developers for good ES5 and ES6 use cases: http://speakingjs.com/es5/ch15.html#named_parameters, http://exploringjs.com/es6/ch_core-features.html#_handling-named-parameters, http://exploringjs.com/es6/ch_parameter-handling.html#_named-parameters-via-destructuring, http://exploringjs.com/es6/ch_parameter-handling.html#sec_named-parameters
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!