tc39/proposal-logical-assignment

introduce only ??=

humanchimp opened this issue · 3 comments

Thanks for this proposal.

Anecdotally, I have experience with CoffeeScript, which has all the flavors of logical assignment operator.

I don't think I ever used &&=, and I don't recall seeing it used often.

On the other hand, ??= (which is spelled ?= in CoffeeScript) was commonly useful, and does seem to me (if we're taking votes) worth the complexity it would add to JavaScript.

The other point I want to make is that I recall seeing ||= used often, but it usually seemed like ?= would have been a slightly better choice in most of those cases. I got the sense that ||= was used due to familiarity with the javascript idiom of using || as a default operator, and the programmer just didn't know about ?=.

Have we considered just adding ??= and not the others? You could argue for consistency, but it's my opinion that &&= is not very useful, and that ||= is an attractive nuisance, which people will reach for reflexively and will use to write code that is in some cases incorrect.

There is quite a lot of JS code out there containing subtle bugs caused by treating || as the default operator, and don't the same caveats apply to ||= as apply to using || as a default operator?

There’s just as much code properly using || to select a value based on truthiness - I’d use ||= quite often for those use cases.

I don’t think adding an operator piecemeal is a better outcome than adding a complete set, even if some of the additions are used more rarely.

I’ve needed &&= inside for loops a few times. And with ||=, I’m not always trying to initialize some variable.

I don’t think adding an operator piecemeal is a better outcome than adding a complete set, even if some of the additions are used more rarely.

👍

There is quite a lot of JS code out there containing subtle bugs caused by treating || as the default operator, and don't the same caveats apply to ||= as apply to using || as a default operator?

I think that learning and teaching the difference between || and ?? is a better strategy than attempting to force clueless coders to write bugfree code in spite of themselves. Or, taking a more pessimistic POV, I think that people not understanding the difference between ||= and ??= are likely to write buggy code anyway.

An important point is that we introduce ??= not later than ||=, so that people will have no good reason to use the latter when they mean the former.