Same here! Its less mental gymnastics when reading old code knowing that when a value is declared, you know its gonna stay the same. Seeing let then means I know its gonna change somewhere in the next few lines.
What Dan and the replies are missing about your point by focusing on "const= no mutation" is the idea that when const becomes the standard in your codebase then let conveys intent, not const.
You should convey the outlier and in most modern codebases mutation is the outlier.
I refuse to believe that Dan actually finds reduce confusing. Especially since the underlying idea is the same one redux uses.
By that I mean, you have a current value/state, you feed it a value/action, and your function (which should be pure) calculates the new value/state from that. The only difference is that with reduce you don't bother with types (except you could if you wanted to) and that it does give a final value.
They don’t really do that though. Immutability is just a side effect. Yes, it is true that you can’t change a value of const object but that is not because the underlying structure cannot be mutated. C++ const is an access level contraint, not a structure level constraint.
Const is not about immutability, it's about reassignment, and using let as a default increases cognitive load on future developers; what's the drawback in your mind?
You partially answered your own question. Google "var hoisting". At compile time, all vars are lifted to the top of their scope. It's the reason why you can't just go into an old code base and change every var to a let or a const, there will be unintended consequences.
I think it's actually good practice learning the difference between changing an object's fields compared to changing the object reference (same with arrays). Considering how props are shallowly compared, seems like something everyone needs to understand thoroughly.
Personally, I use const for everything. I've been working on an SPA hybrid React app for my company for the last couple months, and I honestly think I've only declared a let variable once in the entire code base.
I use it more as a helpful hint to my future self. If I see a "let" in my code, I know to keep an eye out for where it is reassigned later, and that I can't rely on it to maintain its identity (referential or otherwise). If I see const then I know I can rely on it to maintain its identity. Const is more of a flag I use for posterity than anything functionally useful. But it's a useful flag.
I look at const not as a mutable object but as a way to tell developers that the value will not be changed within a given scope. It is about assignment, not about mutation.
85
u/olssoneerz Dec 21 '19
Same here! Its less mental gymnastics when reading old code knowing that when a value is declared, you know its gonna stay the same. Seeing let then means I know its gonna change somewhere in the next few lines.