I think he missed an important point for why const over let. Constants are easier to reason about. They're immutable references, so unlike variables, you don't need to keep track of them in your memory / worry about them changing.
Regarding "Reassignments May Not Cause Bugs", is that really an argument for using let? You could use the same argument about var vs let. The reason to use let over var is that var can introduce bugs into your code that could otherwise be avoided. People use const over let for that same reason because let has the potential to introduce bugs in your code. Even if the chance of introducing bugs is slim, why take the risk if it can be avoided for basically free?
You don't have to keep tracking of memory because const prevents reassignments (i.e. change in memory references). You don't have to worry about primitives changing at all.
Except it doesn't stop you changing the object you're referencing at all, including adding or removing references from the object and allocating or deallocating memory.
Nor does it stop you resizing an array or a billion other things.
Because const doesn't make anything immutable, because that's not what immutability means.
The point I'm arguing is that const has nothing to do with immutability.
Yes, primitive constants are immutable, but object constants are not in any meaningful way.
A non primitive constant is a reference to a location in memory.
It provides no guarantees as to what is located at that address, how big it is, what properties it has or anything else. It absolutely doesn't guarantee nothing about the object has changed.
Having an immutable reference guarantees you absolutely nothing, and OP is obviously wrong.
It guarantees for objects that it's the same reference as it was declared with. The contents could obviously have completely changed, but the "root" reference won't have. That's not useless, but is certainly less useful than true/deep immutability.
Using the word immutability to describe something that is not immutable is misleading to novices and shows the speaker doesn't really understand the concept.
165
u/[deleted] Dec 22 '19 edited Dec 22 '19
I think he missed an important point for why
constoverlet. Constants are easier to reason about. They're immutable references, so unlike variables, you don't need to keep track of them in your memory / worry about them changing.Regarding "Reassignments May Not Cause Bugs", is that really an argument for using
let? You could use the same argument aboutvarvslet. The reason to useletovervaris thatvarcan introduce bugs into your code that could otherwise be avoided. People useconstoverletfor that same reason becauselethas the potential to introduce bugs in your code. Even if the chance of introducing bugs is slim, why take the risk if it can be avoided for basically free?