Skip to content

Comment on ES6 in Depth: Destructuringparent

Comments

Why throw an error for something that isn't needed? This is always something I like about JS... it doesn't throw an error in many situations, where the most likely case would be to suppress it anyway, or when there's no point in throwing one...

Why throw an error for something that isn't needed?

Because it appears the programmer's assumptions have been violated at this point. There's a syntactical way for destructuring assignment of a variable-length list, and it wasn't used. Maybe the programmer was being lazy, or maybe the programmer's assumptions were violated.

How do you feel about

    [a, b, c, d] = [1, 2]
? Should it throw?
it doesn't throw an error in many situations, where the most likely case would be to suppress it anyway, or when there's no point in throwing one...

PHP rightly gets lots of flak for attempting to lumber on after the programmer probably made a mistake. JavaScript isn't as bad, but it does hide a fair number of programmer mistakes. For small web apps where failure or incorrect data isn't a big deal, maybe hiding small bugs is the right thing. For large applications, hiding likely programmer laziness or minor bugs is not a good feature.

I tend to be a bit more meticulous than most when it comes to structure and organization of software projects (I'm downright anal), Though I do see your point... I've always just found it easier to work with the JS behavior, and simply verify critical data at critical points.

In terms of your example, in JS, I would expect c and d to be undefined. Though I sometimes wish that JS hadn't made the distinction between undefined as a value vs. null. However, the JS flow makes it easier to do something like...

    let [foo, bar, baz, optional] = getConditionsFor(id);
Where you expect certain values to only sometimes be there conditionally... To me this is flexibility that I appreciate, and once you are used to the JS way it becomes a lot more natural.

At this point I've written a lot of code in JS both on the server and the client... after 3 years of mostly node/js projects, I've spent the past week in .Net land and frankly I miss JS, though the JS on the web portion of this project is almost as painful. It's amazing how many times a small piece of coupled code can by copy/pasted instead of isolating it for reuse... Spending a week cleaning up such code to get a handle on a number of bugs, not fun.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.