Hacker News new | ask | show | jobs
by cbaleanu 3203 days ago
Bad article, the author should have indeed put some more time into understanding what he is writing about.

  var firstTeenager = people.find(teenager)
Stuff like this, when you write about ES6 features but use var makes my eyes hurt :(
2 comments

I agree that the article needs some work, but there really is nothing wrong with still using var. I'm in the Kyle Simpson camp and just use var for everything except if I'm actually creating a constant or block scoping comes into play or I need to prevent the hoisting of a variable. That way, it's clear what I'm doing and what my intentions are.
> That way, it's clear what I'm doing and what my intentions are.

It's the exact opposite since you're defaulting to the most permissive binding style.

Defaulting to `const` would actually make it clear what your intent is: you use `let` if you need mutable bindings and you use `var` if you need hoisting.

If you default to `var` your intent is apparently to make use of mutable bindings and hoisting everywhere, at which point I can only expect that you put your variable declarations at the bottom of your function blocks after the `return` statement.

Defaulting to const does nothing. If you or someone else decides later that it should be reassignable, then they'll change it and not think about it. If const is rare in your code, people will be aware of what const means in that context.

If you're putting your variable declarations after your return statement, then you probably have other problems. I also don't know why you would ever expect someone to do that.

> Defaulting to const does nothing. If you or someone else decides later that it should be reassignable, then they'll change it and not think about it.

Which they'll do regardless. `const` is an indication that the binding is not updated in the rest of the scope and thus that said scope can be perused without needing to consider an update to the binding itself. It is a guarantee of SSA, nothing more and nothing less.

> If const is rare in your code, people will be aware of what const means in that context.

The same as above which you apparently assert is "nothing"?

> If you're putting your variable declarations after your return statement

Why would I do that? I'm not adamantly using `var` when I have no reason to.

If everything is a constant then nothing is a constant.

Code bases change and some things that a coder did not foresee needing to be reassignable may later need to be reassignable. There aren't frequent cases that switching them becomes a problem, and as a result, people switch them. Since they do get switched so often, people stop thinking about when it really matters or if there was some intent to it originally being written that way. If everything is a constant, then the cases where it matters and the cases where it doesn't get blended together and it's impossible to differentiate the two.

When real, true constants are the only things declared with const, then it's abundantly clear what the intention of the coder was and that the value should never be changed nor should it ever be switched over to let/var.

Likewise with let, in the few cases where it's used it draws others' attention to the use of block scoping or the avoidance of hoisting. It's rare that those things really come into play, so the few cases where it's present really stand out.

tl;dr Block scoping and constants are the exception, not the DEFAULT, so you should write code accordingly.

> If you're putting your variable declarations after your return statement

What I meant here is that people shouldn't keep using var just so they can continue writing bad code.

> There aren't frequent cases […] Since they do get switched so often

They don't. In fact they almost never do.

> tl;dr Block scoping and constants are the exception

That's literally the opposite of reality. Constant bindings (single-assignment) and block-scoped bindings are by far the most common state of affairs even if not formally stated.

> What I meant here is that people shouldn't keep using var just so they can continue writing bad code.

Maybe you should tell the you of two comments ago who's apparently arguing for exactly that?

I think a good case can be made that examples meant to illustrate a single new feature are better if the only new features they use are that one and any others that are necessary to make a reasonable example.