GithubHelp home page GithubHelp logo

Comments (6)

vsemozhetbyt avatar vsemozhetbyt commented on May 5, 2024 3

Some info to consider:

  1. Object arguments are de facto standard. For example, they are used in many Node.js API functions .

  2. 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.

ryanmcdermott avatar ryanmcdermott commented on May 5, 2024 1

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.

andreaselia avatar andreaselia commented on May 5, 2024

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.

jordyvandomselaar avatar jordyvandomselaar commented on May 5, 2024

Please read 1,2 and 3. We have had this discussion on Slack.

from clean-code-javascript.

andreaselia avatar andreaselia commented on May 5, 2024

@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.

jordyvandomselaar avatar jordyvandomselaar commented on May 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.