4

I've really fallen in love with object destructuring with functions.

For example:

var buyCoffee = function({sku, pounds = 1, roast:''}){
 ...more code
}
buyCoffee({sku:"cf-100" pounds: 3, roast: 'dark'});
buyCoffee({sku:"cf-101" roast: 'light'});

Pros

  • Flexibility similar to the args object.
  • Added Simplicity
  • Not required to put in parameters if I don't need them.

Cons

  • Variable names are locked all the way through.
  • Currying would be much harder.(From what I can tell)
  • Significant computational overhead vs traditional params ?
  • Harder to test?

I'd like to know what downsides there are to this approach? Is this a good pattern to use as I grow as a developer? Just looking for some wisdom from the trenches on this. Thoughts?

Justin
  • 2,940
  • 3
  • 25
  • 43
  • The main downside I have encountered in my own time is that you don't have reference to the object that is being destructured. – sdgluck Dec 12 '16 at 17:09
  • *"I've really fallen in love with object destructuring with functions."* – careful with things like this. You'll end up using it where you shouldn't. – Mulan Dec 12 '16 at 17:39
  • I agree, that's actually why I'm asking the question :D As to try and avoid trendy programming methods. - I noticed some votes to close, that the question lends it self to being more opinion based. How might I post this question in a way that would facilitate more factual responses? Maybe this type of question just isn't really fit for stackoverflow? Thoughts? – Justin Dec 14 '16 at 16:04
  • I just voted to close my own answer, listing it as a duplicate of http://stackoverflow.com/q/12826977/1048572 Mentioned by @Bergi below. I think gets the heart of this issue more. – Justin Dec 14 '16 at 16:15

1 Answers1

6

Variable names are locked all the way through.

Not at all. You can easily destructure into arbitrary variables:

function({sku:mySku, pounds:localPounds=1, roast=''}) { … // use mySku, localPounds and roast

Currying would be much harder.

There's no currying when you pass objects anyway. And currying with optional parameters is always hard.

Potential computational overhead?

Depends on what you compare it against.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1. Good point, about renaming parameter names, didn't think about that. 2. Yeah agreed. 3. I'd be comparing against using tradtional paramter scheme func(param 1, param 2) {} – Justin Dec 12 '16 at 17:19
  • 2
    I think [that's a different question](http://stackoverflow.com/q/12826977/1048572) than "Should I use destructuring for option objects or not". – Bergi Dec 12 '16 at 17:28
  • That's actually a way better answer to this, thanks Bergi. – Justin Dec 14 '16 at 16:12