News for hackers. Stuff that matters.
Re:Constantly Confusing ‘const’ by pornel on Tuesday September 08, 2015 @03:36PM (#10187736)
I don't agree with this style.

`var` is broken. Don't try to find a use-case for it. Just don't use it.

It would be nicer to have real, deep immutability, but `const` isn't it. Therefore, use it for what it actually is, not what you wish it was. If you don't intend to reassign a reference, then mark the reference constant. That's it.

It may be surprising, but it's quite easy to write JS where almost every declaration is `const`. When every mundane variable is `const`, then the more complex initializations/data flows/accumulators really requiring `let` stand out, which is very useful for reviewing code ("let" = "warning: there may be an `else` or missing `switch` case that will leave that undefined").

Because `const` requires to be initialized at the time of definition, you know the variable is valid for its entire lifetime. When you find its value was wrong, there's only one place where the value could have come from (you don't need to comb the function to look for reassignments).

Re:Constantly Confusing ‘const’ by _getify on Tuesday September 08, 2015 @04:09PM (#10187942)
> `var` is broken

Nonsense. Revisionism at its finest.

But that's an argument I made in a different blog post: http://davidwalsh.name/for-and-against-let

> It would be nicer to have real, deep immutability, but `const` isn't it.

Yep, agree with you on that. That was one of the main points of my post.

> Because `const` requires to be initialized at the time of definition, you know the variable is valid for its entire lifetime.

No, that's where you're mistaken. All you really know is that the variable assignment is reliable. If the assignment happens to be of an immutable value, great.

But given the choice between these two guarantees:

1. The assignment is immutable

2. The value is immutable

(1) is almost pointless -- and a quick scan of your small block where it's scoped will verify that or not.

(2) is the absolute important one.

`const` masquerades as (2) when it's really only (1). That's what I don't like about it.

Re:Constantly Confusing ‘const’ by Retra on Tuesday September 08, 2015 @05:01PM (#10188184)
Your blog post doesn't make a very strong argument, honestly.
Re:Constantly Confusing ‘const’ by _getify on Tuesday September 08, 2015 @05:19PM (#10188267)
I would genuinely (no sarcasm) like to read a blog post that makes a stronger argument (in either direction) than mine. Most blog posts I've read about `const` cite very flimsy things like "it makes my code more readable" without any real meat as to why that's true. I tried to debunk that with careful thought. If someone has more convincing arguments besides subjective opinion, I'd love to read them.
Re:Constantly Confusing ‘const’ by Dylan16807 on Tuesday September 08, 2015 @06:18PM (#10188512)
>I tried to debunk that with careful thought.

Well you didn't really succeed. Your argument against using const comes down to "someone might violate expectations or remove the word const, so let's not even try". I don't agree with that.

Traditional constants are used to show that nothing changes in large swaths of code; showing how much benefit you get in a standalone 12 line function is not meaningful.

Also, calling function scoping bad is not revisionism. That is not a new idea, and does not depend on 'let' existing.

Edit: And the the article you linked about let is saying so many ridiculous things I won't even try to respond to it, but jeez. 'implicit'

Re:Constantly Confusing ‘const’ by _getify on Tuesday September 08, 2015 @07:40PM (#10188833)
> Your argument against using const comes down to ...

You did an absolutely horrible job of summarizing even remotely what my argument was, so I guess "I won't even try to respond to" this, since you either can't critically read or didn't care to this time. jeez.

Re:Constantly Confusing ‘const’ by Dylan16807 on Tuesday September 08, 2015 @08:25PM (#10188971)
That's what you devote several paragraphs to. I'm not sure how else you explain quotes like "the likelihood is that a developer who needs to change a variable and gets a const-thrown error about it will probably just change the const to let and go on about their business."

I don't care to argue about 'let' because this is a post about 'const'. I was writing a rebuttal in the previous comment but realized it was way too off-topic. But your definition and reasoning for "implicit scope" hurts me to read.

Re:Constantly Confusing ‘const’ by dsjoerg on Tuesday September 08, 2015 @05:51PM (#10188406)
When you say "don't use [var]" do you mean only in ES6, or even before ES6?

If before ES6, then if you could explain your reasoning more I'd be interested to hear it. I use var regularly, but would be happy to be re-educated.

Re:Constantly Confusing ‘const’ by orangeshark on Tuesday September 08, 2015 @06:32PM (#10188573)
There is no alternative to var before ES6, so of course your only option is to use it. If you use any of the various compilers that introduce let and const, it is recommended to use them over var. These compilers basically just check, at compile time, those variables satisfy the correct behaviors of let and const and just converts them to var for ES5 and below.