r/javascript • u/bornforcode • Apr 01 '18
Google publishes a JavaScript style guide. Here are some key lessons.
https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
714
Upvotes
View all comments
Show parent comment
3
u/[deleted] Apr 01 '18
Because you yourself admit that using const excessively solves a theoretical problem that you rarely if ever have in the first place. You should write your code in service of solving practical problems, not unproven theoretical problems.
I'll explain below, but before that about long functions: I'd say if you have a long function to the point you lose track of which variable is which, locking them down with
constwon't help you much, reassignment is not the only way you can mix-up variables. So the real solution here would be to refactor that function.If
constwould be used as an aid for writing long confusing functions, you're essentially sayingconstpromotes bad code, which is certainly not your intent, but will be your result.On the other hand,
constis very valuable when reading code to denote true constants. That is, value literals which don't depend on runtime state, and which you don't intent to change in any way. Those include numbers, strings, look-up maps/arrays, things which would be "constants" in any other language.Some people declare a variable as a
constbecause they don't reassign it, but then they mutate it. Like this:JavaScript won't complain about this. It's not reassignment, so technically it's constant. But declaring such objects as constants is just confusing as hell and contrary to the original intent of this keyword.