r/javascript • • 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

317 comments sorted by

View all comments

Show parent comments

15

u/ghostfacedcoder Apr 01 '18

Why not? I can accidentally assign to a variable inside a function can't I? Const vars prevent that from happening, and cost you nothing (except having to type one extra character vs. let).

0

u/[deleted] Apr 01 '18

I can accidentally assign to a variable inside a function can't I?

Yes, this was such a common mistake to make before const.

Right?

5

u/ghostfacedcoder Apr 01 '18

Not common, no. In fact, with the way I code functions are rarely even big enough for something like that to happen.

But at the same time, all it takes is one long function (your's or your co-worker's) and one var to potentially introduce a time-consuming bug, so why not use const? Other than the two extra characters there is absolutely no downside whatsoever to using it.

As soon as you adjust to using const instead of var it just becomes a habit, and then you're protected everywhere you define a variable by habit.

3

u/[deleted] Apr 01 '18

But at the same time, all it takes is one long function (your's or your co-worker's) and one var to potentially introduce a time-consuming bug, so why not use const?

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 const won'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 const would be used as an aid for writing long confusing functions, you're essentially saying const promotes bad code, which is certainly not your intent, but will be your result.

On the other hand, const is 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 const because they don't reassign it, but then they mutate it. Like this:

const list = [1, 2, 3];
....
list.push(4);

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.

4

u/ghostfacedcoder Apr 01 '18 edited Apr 01 '18

const !== immutability; that's true. And shorter functions > longer functions is also true (well, not in the most literal sense, but you get the idea).

But for any programming construct, if you have two equally valid options, and one has benefits the other doesn't, why use the inferior option? Using const in no way stops you from defining more traditional SOME_VALUE = 5 constants, nor does it stop you from using let (or even var if that's your thing). It just prevents a possible bug, and even if it only prevents said bug 0.01% of the time ... why not? Especially when that 0.01% of the time bug can be hard to debug and can eat up a decent bit of your time if it does occur.

-1

u/[deleted] Apr 01 '18 edited Apr 01 '18

But for any programming construct, if you have two equally valid options, and one has benefits the other doesn't, why use the inferior option?

I already explained that.

Denoting constants is useful in practice. And locking down every variable you can, even when it's initialized from runtime state, or mutated afterwards, is useful in theory for something you never experienced as a problem. You can't claim a "benefit" if you don't have the problem the benefit is supposed to solve.

You're giving up one potential practical benefit, in order to focus on a theoretical unproven benefit.

Using const in no way stops you from defining more traditional SOME_VALUE = 5 constants.

So we have the concept of "more traditional constants" and "more modern constants" in the same language. Is that a good thing for communication and code readability?

If someone tells you "foo is a constant" which constant do you imply they mean if the code is not right in front of your face? Will everyone infer the same meaning? Or will we get stuck clarifying which is which every time, like we're doing right now in this thread? This is not smart.

6

u/ghostfacedcoder Apr 01 '18

As I see it, choosing const costs you nothing, prevents some bugs some of the time (but again, not a lot of the time), doesn't have any downsides (again, you can still declare "all caps" module-level constants all you want) AND it is widely regarded as preferable in the JS community.

But obviously nothing will explode if you use var or let by default (over a decade of JS programming worked just fine with only var), and at worst you'll just waste some time, so if you have such a passionate aversion to const don't use it.

You clearly like doing things your way, so I won't try to convince you any further.

-2

u/lordxeon Apr 01 '18

Function level variables

I don't think variables declared inside a function should be declared with const. Any global, or global equivalent are fine being const if necessary.

And, they do cost you something because unless you're running the ES6 code by default, it's being converted back into vars by babel anyway.

2

u/ghostfacedcoder Apr 01 '18

Maybe now, but that's going the way of the dodo/IE box model. Chrome already supports const and let, and I'm pretty sure the other major browsers do too, so it's only a matter of time until all the older versions fall out of use and const/let won't require transpilation.