4 ms·
IMO the article's conclusion is wrong. We should indeed use `const`, and we should indeed use it to communicate. But the thing we should use it to communicate i
by eyelidlessness 7y ago
IMO the article's conclusion is wrong. We should indeed use `const`, and we should indeed use it to communicate. But the thing we should use it to communicate is different than what the article proposes.
We should use `const` to communicate that we don't intend to reassign or mutate the variable. And we should do that by default unless there's a compelling reason to do otherwise.
I know it can't be fully enforced, because JavaScript is designed for mutability of objects by default. But it is an implementation style that is almost always achievable in plain ES6 without too much trouble, and it makes more reliable software that's easier to reason about.
- _bxg1 7y agoAnd in TypeScript you can at least pair it with Readonly<...>, though that unfortunately still isn't recursive
- ht85 7y agoNice plugin to make sure you never mutate: https://github.com/jonaskello/tslint-immutable/tree/master/test/rules/no-object-mutation https://github.com/jonaskello/tslint-immutable/tree/master/t... You can add a rule based on the name of the variable to allow mutating (e.g. naming it `mutableSomething`)
- _bxg1 7y agoInteresting, but not really tractable if you have to manually add every single variable to the rules config.
- chaorace 7y agoThe plugin allows pattern-based exceptions. For example, if your rule excluded variables with a "mutable" prefix, you could name your variable mutableSomething and it will be automatically ignored by the linter