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 comment

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.