5 ms·
I agree lukifer. Immutability is useful and interesting, but `const` in JS not really about immutability, it's about variable reassignment (which is trivial to
by sunseb 6y ago
I agree lukifer. Immutability is useful and interesting, but `const` in JS not really about immutability, it's about variable reassignment (which is trivial to handle really, even for a noob programmer).
const arr = ['foo'];
arr[0] = 'bar';
assert(arr[0] === 'bar'); // !!!
Something like that would make sense though IMHO:
const PHI = 1.618;
I don't get how JS is designed really, they try to solve a bad design (`var`) but they introduce another bad design (`const`) and they make things even messier. Many devs ship JavaScript thought TypeScript these days, so I hope Microsoft can clean this mess one day.
- madeofpalk 6y agoWhat about const/let is poorly designed?
- sunseb 6y agoHave a look here: https://jamie.build/const https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080 https://twitter.com/dan_abramov/status/1208369896880558080 (React core team member) https://twitter.com/littlecalculist/status/917875241891676160 https://twitter.com/littlecalculist/status/91787524189167616... (Ember creator)
- madeofpalk 6y agoI don't think it's poorly designed, its just maybe perhaps not all that useful?
- karatestomp 6y agoSome people seem really unhappy that it might mean a reference is constant, rather than a value, if the value happens to be a reference. If that makes sense.
- TechBro8615 6y agoThe issue I have with `const` is not your original premise, but the one you allude to here: people confuse reassignment and immutability. Because of this, they end up using const/let to "signify that they're not changing a variable," when it doesn't actually matter anyway, because they're thinking of immutability but disambiguating reassignment capability. You can usually tell who has only a superficial understanding of the language by reading how `const` and `let` are used in their code. Personally, I default to const, and use it pretty much 99.9% of the time. I use `let` in the few cases where it's actually useful to have a block-scoped variable. Ironically, this probably does a better job of "communicating" intent than the ritualistic obsession with "immutability" does, simply because you know if you're deviating from the default, there is a good reason for it.
- lukifer 6y agoYeah, that's fair, I've been bitten by "not actually immutable" bugs before (works well enough for primitives, but not objects or arrays, which is most things in JS-land!). Libraries like Immutable.js can help bridge the gap. > they try to solve a bad design (`var`) but they introduce another bad design (`const`) `let` is intended to be the actual `var` replacement, and it's okay for that. I think there's an argument that `const` is a bad design, in the sense that it's deceptive, under the not-that-uncommon case you describe. Still, I'd rather have it than not, and I personally still like defaulting to `const` and opting-in to `let` in the (rarer and rarer) event that it's needed. (I'm also on 100% TypeScript at this point, which doesn't remove the potential for problems, but does provide an extra layer of guard rails in general.)
- sunseb 6y agoYes, `let` is fine. I use `let` 99% of the time in my personal projects, it's more convenient, but I often must use `const` when working on an existing codebase. :( Most of JavaScript devs do like you, they default to `const` and if they need to reassign the variable, they switch to `let`. To me it feels more like a ceremony for declaring a variable than a rational and a useful practice. :)