Comments (6)
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
from clean-code-javascript.
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!
from clean-code-javascript.
I disagree.. (in this case) an object makes sense rather than individual args. the following Stack Overflow link provides some good insight: http://stackoverflow.com/questions/12826977/multiple-arguments-vs-options-object
from clean-code-javascript.
Please read 1,2 and 3. We have had this discussion on Slack.
from clean-code-javascript.
@jordyvandomselaar I read your post, but it appears as though you're commenting on personal preference rather than what makes sense.
from clean-code-javascript.
It makes sense to use actual arguments because the function will explain itself. If you use an object the user would need to read documentation before he or she understands all options inside the object you're using. With proper argument names your ide will tell you exactly what goes where, you can even add JsDoc for additional information. An object does not allow this.
I dare say the same to you; do you just want to use objects because objects, or because it makes sense?
from clean-code-javascript.
Related Issues (20)
- The code example of <Don't ignore rejected promises> may be wrong.
- Nice HOT 4
- Contradict HOT 1
- De code HOT 5
- Js two HOT 1
- eslint plugin
- Add OSSF Scorecard security workflow
- Clean code HOT 1
- Close Issues with no description/details attached. HOT 1
- J HOT 1
- J
- I
- Clean HOT 2
- Js HOT 1
- Clean HOT 1
- Adding translations HOT 3
- CleanCode HOT 2
- JavaScript codes flower
- Query regarding code HOT 2
- I am lerning
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clean-code-javascript.