yoshuawuyts / es2020

Because in hindsight we don't need most of ES6
MIT License
126 stars 14 forks source link

Default parameters #12

Closed rtsao closed 8 years ago

rtsao commented 8 years ago

I think default parameters are preferable to common patterns such as:

Compact, but loosey goosey boolean coercion

function foo(bar) {
  bar = bar || 'baz';
}

or

Explicit, but ugly

function foo(bar) {
  bar = typeof bar !== 'undefined' ?  bar : 'baz';
}

I think default parameters are much more explicit and avoids a mutating assignment:

function foo(bar = 'baz') {

}

In conjunction with object destructuring you also have a nice defaults pattern: https://gist.github.com/ericelliott/f3c2a53a1d4100539f71

Of course you can always use xtend but the compactness is nice.

yoshuawuyts commented 8 years ago

They thanks for taking your time to open this. This is the exact format I was hoping people would use to open issues. I'm not a fan of default parameters, because when it comes down to it, it's just sugar.

And as we all (should) know sugar is a form of carbs, and carbs are what create that nasty bloated feeling. So we must restrain ourselves and not dump all the sugar the TC39 cafeteria person put on the table on our otherwise healthy course.

As pointed out in https://github.com/yoshuawuyts/es2020/issues/1#issuecomment-221376737, default parameters can create some nasty combinations with other pieces:

var fn = ({foo = 3, bar = 4} = {}) => ()

So to sum it up: I'm not a big fan of adding this. I'll leave this issue open for now so it perhaps others can chime in. Thanks!

simplesmiler commented 8 years ago

Why not?

if (bar === undefined) bar = 'baz';

You don't need typeof undefined here because the variable is definitely declared.

bendrucker commented 8 years ago

@yoshuawuyts Want to start making final decisions on the open proposals? Happy to add my final vote on anything open. I'm 👎 on default params.

yoshuawuyts commented 8 years ago

Yeah, I'm pretty happy with es2020 as actually - no language-changing features; nothing that can be greatly abused

bendrucker commented 8 years ago

Same. I'm 👎 on all the current feature proposals.

ahdinosaur commented 8 years ago

after more time spent going back and forth, i'm :+1: on #1, #3, and #4. as in, here's what i've come to use in most projects, so due to how babel is modularized (supporting destructuring function arguments includes supporting default parameters) this feature request would be included. if y'all want to keep es2020 as is (which is cool, i quite like how minimal it is), happy to make a es2040 or something for those of us with fuzzier vision.

yoshuawuyts commented 8 years ago

@ahdinosaur oooh, I like the es2040 name; seems pretty cool. Or es2021 just because it's the next in order haha :sparkles:

ahdinosaur commented 8 years ago

@yoshuawuyts done: es2040 and babel-preset-es2040. :smiley_cat: here's the babel transforms i added.

yoshuawuyts commented 8 years ago

cooool! Means we can close this issue; PR for link to es2040 in the readme welcome :sparkles: (My brain is dust; can't write for shit rn haha)

yoshuawuyts commented 8 years ago

yay for https://github.com/ahdinosaur/es2040 :sparkles:

bendrucker commented 8 years ago

Nice, I like the idea of keeping 2020 as the best 3 major features (arrow fns, template tags, const) and then having 2040 support the better syntactic sugar features. That leaves a clear path to recommend for gradually adopting new features and figuring out what level of sugar you want: