3 ms·
Interesting to read about variable mutability here: https://verdagon.dev/blog/on-removing-let-let-mut https://verdagon.dev/blog/on-removing-let-let-mut Origin
by kitd 4y ago
Interesting to read about variable mutability here:
https://verdagon.dev/blog/on-removing-let-let-mut https://verdagon.dev/blog/on-removing-let-let-mut
Originally there was let and let mut like Rust. But that was changed to simple assignments, followed by the compiler checking for subsequent reassignment with set.
While I can see that might make for cleaner code while still allowing the compiler to get on with its job, surely one of the benefits of explicitly immutability is to stop the coder accidentally letting off foot guns. If simply setting a variable after declaration makes it mutable, it feels like it could open the door to data in a code block being allowed to change behind the scenes when the assumption in that block is that it is fixed.
I'd be interested to know how this works in every day practice and possible unintended effects.
- junon 4y agoAgreed, I otherwise love the language just at face value but this was a big "ehhh" for me. I'd rather be boxed in and have to opt-into increasing bug surface area.
- christophilus 4y agoAgreed. Seeing const vs let when reading / writing TypeScript gives me a clear, explicit indication of intent. I like having this be explicit.
- prirun 4y agoPersonally, I don't like these two kinds of syntax for declaring and assigning a mutable variable, because if code is moved around - which happens all the time - I have to switch the syntax for declaration vs assignment. Same thing with Go's := and =.
- imtringued 4y agoThe vast majority of variables in your program won't be mutable or be changed around so why add the mental burden of mutability on every single variable just for some tiny minority of cases?
- izackp 4y agoIt doesn't have to be a mental burden.. Just write all your variables as mutable then let the ide automatically correct them to immutable. Ex: "Warning: This variable is never mutated. Would like to make it a const? <Fix>" Let's say it is a mental burden. I'd rather have the burden when writing the code over when reading the code. (https://news.ycombinator.com/item?id=31823991 https://news.ycombinator.com/item?id=31823991)
- izackp 4y agoI would definitely prefer swift's 'var' or JavaScript's 'const' here. The solution in the link adds to cognitive complexity. Imagine you had bad code (Worst case scenario; which always happens). A function with 400+ lines of code. You would have to scan the entire function just to figure out if a variable is mutable then you would have to force yourself to remember _all_ the variables that are mutable when evaluating behavior.