4 ms·
Author here {: Yes, what you are saying here is what I meant. I didn't want to mix in the whole topic around "passing by value or reference" in this short tease
by rwieruch 8y ago
Author here {: Yes, what you are saying here is what I meant. I didn't want to mix in the whole topic around "passing by value or reference" in this short teaser.
Hi Swizec :wave: :)
- pygy_ 8y agoHi, Robin. Then you may want to say that `const` guarantees that the name will remain bound to that object. The great-GP is correct in that `const` has nothing to do with immutability besides that. It doesn't necessarily conveys an intent of inner immutability. It just states that whatever is in the variable will stay there until it goes out of scope. Using `const` whenever possible is good advice, off course.
- rwieruch 8y agoGood point, thank you for the clarification! Somehow I have always thought about showing the intent of keeping the data structure immutable when using const. I have changed it in the article. One more learning for me today :)
- Silhouette 8y agoI've found this is often a problem in languages that pass simple values by value but compound values like objects and arrays by reference. The meaning of a qualifier like const and the inference a reader can draw when they see it can fundamentally change depending on what type of value it is applied to. My experience has been that both the change in value/reference behaviour and the lack of total immutability guarantees when using a const qualifier on a reference type can be a source of bugs. Languages like C and C++ distinguish more explicitly between value and reference semantics, and likewise between constant pointers and [mutable] pointers to constant data. To some extent that removes those sources of bugs, at the expense of having to write more explicit but verbose types like `const SomeType &` instead of `SomeType` all over the place.
- ravenstine 8y agoIt's been astonishing to me how people switched from var to let, but seem confused as to when to use const. I use const wherever I can since it's more appropriate for non-changing variables, and it helps me more easily visualize how a variable is being used in a block. But in other people's code I see a lot of letting all over the place. Maybe it's because of those articles saying to use let instead of var, but glossing over const.
- BigJono 8y agoIt's trivial to learn as well. You use const for everything until you get an error for trying to reassign it.
- vga805 8y agothe only way to go
- mygo 8y agomaybe it’s because const is 2 more letters to type. if “let” was “letitbe” I bet const would be more popular seriously though, I wish they could have chosen a 3-letter word for const in keeping with var and let. I know const exists in other languages, but it doesn’t even mean the exact same thing as some other languages anyway.
- dpwm 8y ago> I wish they could have chosen a 3-letter word for const in keeping with var and let. "set" or "fix" would be quite nice, given what const actually does.
- DonHopkins 8y agoI'm having one of those days when variables won't and constants aren't.
- pygy_ 8y agoIt would have been nice to have `let` work like `const` does, and something else for what is currently `let` (`mut`?). The TC39 was constrained by the list of reserved words though. `let` and `const` were reserved in ES5, and there was nothing that could have conveyed `mut`. https://mathiasbynens.be/notes/reserved-keywords#ecmascript-5 https://mathiasbynens.be/notes/reserved-keywords#ecmascript-...