It's easier to reason about a variable if you know it will always be one value—even if the value may be mutated (which is unfortunate).
`const` is a simpler construct. It has less rules and I would always suggest that using `let` will introduce complexity that only needs to be there when you can identify a genuine need to re-assign a value to your variable.
That said, I enjoyed the post. It's a good read!
> It's easier to reason about a variable if you know it will always be one value—even
> if the value may be mutated (which is unfortunate).
True, but because const, let, and var are lexically-scoped, it’s super-easy to reason about everything that happens to variables. (Excepting bad JS environments where you can declare global constants, of course).If I see PI = 3.14159265 somewhere in a file, it’s not a hardship to assume it will not be rebound, and to notice if it is rebound.
The super-important thing to reason about is value mutability, because that escapes lexical scope. We pass mutable values to library functions containing code we have never read. We integrate with code written by a colleague who is in another time zone and may accidentally change a routine to mutate an array we pass with .push instead of .concat.
“Easier to reason about” is massively important with mutability. Not so much with lexical bindings.
I would disagree that it's super-easy to reason about var, despite being lexically scoped. It still causes an enormous amount of confusion when loops and callbacks are used together.
The PI example only really works because we all know what PI should look like. If you were using someone else's constant value and it accidentally got rebound it would be much harder to know.
The argument I made toward the end of the post is that refactoring from `const` back to `let` is more risky than the other way around.
Moreover, surveying my own code, honestly, the vast majority of my declarations should be `var` or `let`, and only a much smaller amount being `const`. I think a lot of people are rushing blindly to `const` without realizing its full behavior.
> [`const`] has less rules ... using `let` will introduce complexity
I think this is completely backwards. `let` has no special caveats (other than the TDZ thing, which they both have), whereas `const` has this caveat that it's only about assignment bindings and not about values.
Imagine a piece of code that has `const x = 2` that you later refactor to `const x = [2]`. The complexity is, you now have to contend with the `[2]` being a mutable value where `2` wasn't, and `const` sorta pretends to keep you safe but doesn't.
That's the complexity I disfavor.
Imagine a piece of code that has `const x = 2` that you later refactor to `const x = [2]`. The complexity is, you now have to contend with the `[2]` being a mutable value where `2` wasn't, and `const` sorta pretends to keep you safe but doesn't.
If these semantics aren't instantly obvious to you, then that's emblematic of a serious gap in your understanding of programming. I say this not with the intent of shaming, but to recommend that you (and the very many other people with the same lapse) invest some time into developing intuition about reference semantics. The best way IMO is to get initmately familiar with a language that has first class pointers or otherwise explicit indirection. Learn C, get good at C, write something big in C. Focus on the pointer semantics and you'll come out a significantly better programmer in languages where pointers are slightly less immediately visible.
Of course if you're making this recommendation because you work with other people who have a lapse in their understanding of references I can understand that. I would encourage you, however, to try to fix the problem rather than work around it by stunting the language. After all, it's an issue that will crop up everywhere, not just the semantics of `const'.
To your point however, `const` does not imply constant in the sense of other languages (like `.freeze()` does) so it does mean your target future audience is someone who is familiar with some of the nuances of recent JavaScript, an assumption that can be dangerous to make.
For my recent ES6 escapades I've been using the pattern of `const` first and `let` only when mutability of assignment is needed and I have yet to have that bite me in the ass. However this has been small projects and I can't speak for a larger codebase.
Alternatively, for a functional style, you have to have a fairly good reason for using `var` or `let`. I find myself treating them like I'd treat atoms in Clojure. They end up being the mechanism you use to make controlled side-effects.
I'll concede that `const` does have a complexity to understand...initially. However once you understand the distinction between assignment and value (you only have to understand it once), then you're back to a level playing field between both.
From then on, `const` gives you a guarantee about the assignment. Objectively, this guarantee eliminates complexity, you can look at a variable anywhere in a scope and know that it has the same value as at time of declaration (same TDZ exceptions apply). I don't see how this could be seen as backwards.
`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).
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.
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.
As a reader, I know that after reading a const line I don't have to check if someone rebinds this name before each use site -- it's impossible.
Interpreters and static analysis tools use this information to stop you when you write a piece of code that rebinds the name, asking "did you really mean to do that? You said you wouldn't."
This turns out to be almost useless information for most of us, since the real problem is not re-assignment, but mutation of the value (in the case of non-primitives).
`const x = 2` feels great emotionally, but `const x = [2]` has the feeling of safety without any of the guarantee.
This is a classic case (quoted from Crockford regularly) of a utility that it sometimes useful and sometimes harmful (aka not useful), and there's a better option (`let`), so the better option should generally be preferred.
I only use `const` (refactor from `let`) when a piece of code is reasonably complete and I'm pretty sure re-assignment will not ever be appropriate. Guessing at that before writing the code is a premature optimization.
And guessing wrongly and having to refactor back to `let` later is more risky.
Const is perfectly sensible with references to mutable objects too. The latter guarantees that x will always point to the same mutable array. If you pass a reference to the x array to a function which needs to mutate that specific array or observe it for changes, then it may be important that x is never rebound to a different array. Consider the following code:
const x = [5,6,7];
Object.observe(x, function(changes) {
console.log('changes', changes);
});
// ...
// time to clear the array
// WRONG! The code observing changes to the old x array will not see
// this change or future changes to this new array. Because we used const, this
// will trigger an error and immediately show us our mistake.
x = [];
// CORRECT. This mutates the array instead of creating a new empty array.
x.length = 0; T *const x = ...; // Immutable pointer to mutable T.
But not this: T const *x = ...; // Mutable pointer to immutable T.
Even so, I don’t agree with the author’s argument that “const” is to be avoided. It does offer some protection against minor errors, which is not nothing.If this is the core argument to not using 'const', then you are out of luck trying to enforce any coding style. I might as well just delete any code that doesn't make sense.
Is that really true? I would assume the opposite. Most developers have used Java or C# or any of the other languages that have similar reference semantics. I would agree if you qualified that with "beginner".
Long story short: If you want to be annoyed about this feature, then more power to you. I won't be.
That's what Java IDEs do with static final variables.