4 ms·
Immutability has been pretty hot of late, though still controversial. There are several popular languages which encourage or even force immutability (Elixir, Cl
by lukifer 7y ago
Immutability has been pretty hot of late, though still controversial. There are several popular languages which encourage or even force immutability (Elixir, Clojure). What sold me on the idea was Rich Hickey's talk, "The Value of Values": https://www.youtube.com/watch?v=-6BsiVyC1kM https://www.youtube.com/watch?v=-6BsiVyC1kM
I look at it as mostly a safety net: I'd rather use const by default, unless I have a good reason to be able to mutate the variable. (To flip it around: what problem does being mutable-by-default solve?) 9 times out of 10, I don't need to change the value for the lifecycle of the function; and often if I do, the code is cleaner anyway if I use an immutable pattern (such as `const foo = bar.map()` rather than `for(x in bar) foo.push()`). Thanks to the magic of Merkle trees, we can generally make fresh pointers without it costing much memory overhead.
- sunseb 7y agoI 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 7y agoWhat about const/let is poorly designed?
- sunseb 7y 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 7y agoI don't think it's poorly designed, its just maybe perhaps not all that useful?
- karatestomp 7y 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 7y 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 7y 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 7y 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. :)