3 ms·
It'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 appropria
by ravenstine 8y ago
It'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-...
- DonHopkins 8y agoHow about balancing out every "let" with a "forbid" at the end of the block to mark the end of the scope?
- eip 8y agoUnrelated but didn't know how to contact you: https://www.youtube.com/watch?v=TdzIKD0oMzo https://www.youtube.com/watch?v=TdzIKD0oMzo
- jwdunne 8y agoI tend to go by the following rules: Use const by default. If I need let, see if I can refactor to using const. I've found that actually I can go far without using let, improving my code along the way. I see a pattern from colleagues. They initialise a variable using let and then branch to figure out what should go inside it. That can be replaced by a pure function. For loops tend to be the next thing. In that case, they can be replaced by higher order functions/methods without detriment to clarity (usually an improvement). Perhaps the move towards iterators, with async support, will make it more common and legitimate. I haven't yet used those in a codebase but perhaps let in that instance leads to clearer code.
- dotancohen 8y ago> They initialise a variable using let and then branch to figure out > what should go inside it. That can be replaced by a pure function. I don't want to detail the discussion, but I would love to see some of your code. I contend with this antipattern often but solve it in different ways often depending on language features. If you're doing anything other than simply calling an auxiliary method with a ton of parameters, I would appreciate seeing your solution in Javascript, a language that I'm learning but not proficient in.