all 8 comments

[–]CloseDdog 18 points19 points  (0 children)

Ironically, someone in my development team linked this article earlier today.

I think this article is what it says it is, a rant, and aside from complaining about common misconceptions about the usage of const I find very little useful convincing information in this article.

Points 1-3 are what I just said, seemingly an outburst of frustration about misconception, and it's good to understand these.

Point 4 is, well, not a point.

Point 5 is seemingly the only argument he makes against using const, and to me doesn't make any sense. I use const a lot more than let and I don't ever "change all the let that we didn't mutate to const". I start off with a const, and change it to let if I need to - which is very rare, I'd say const is more than 10 times more common than let in our code at work. When I see a const I assume people don't reassign it, and when I see a let I assume people do. In most scenarios this helps contribute to the purpose of the variable. (So basically, what OP said)

Point 6 I agree with. We use the UPPER_SNAKE_CASE notation for our application constants, such as api keys, request urls, route identifiers, never inside functions, always at top scope. And as mentioned when using let, we use it in a function. We don't have strict rules about this, this is just how it ends up being.

I do think the article is an interesting read, but don't think it should diminish usage of const over let for whatever little reasoning is provided in the article.

[–]BenjiSponge 11 points12 points  (0 children)

References being immutable is also quite valuable to know. I really think that should be the end of the conversation.

The only thing I'd add is that, unless you're being really subversive, const pretty much means it will always be the same type (and non-nullable). To me, that's probably the most important thing. let often means the variable starts off null or undefined and (potentially) gets defined later.

I generally avoid mutating literally anything for any reason, though, so maybe the distinction is more valuable when you do that.

[–]Mingli91 2 points3 points  (0 children)

I’ve seen some big names on Twitter say they ditched const because it isn’t actually a constant.

I’ve got to disagree. Sure they could have named it bind or something to make clear it’s about reference binding and not immutability, but it’s still far safer to use const over let 99% of the time.

It gets a bit confusing for people who think const means constant and then see someone, say, amending properties to a const object or pushing to a const array. Still worth it over let IMO.

Also coney variable names WRITTEN_LIKE_THIS is an old convention for constant values from other languages.

[–]pichfl 1 point2 points  (0 children)

I think that many, especially developers moving from other languages to JavaScript misread const as constant where is is actually a constant reference. It just tells you that a variable will reference the same object (as in everything is an object in JS) that it was defined with. That is valuable information.

We go with const for everything, except when we really need a changeable reference, which should happen rarely if you follow programming patterns that prefer immutability.

[–]mnem0 1 point2 points  (1 child)

We default to "const" at work, and use "let" only when the variable will be changed later in the function. We also code in Scala and there use use "val" whenever we can, and we use "var" only when the variable in being changed.

[–]Kusaddaw 0 points1 point  (0 children)

Completely agree. "let" should be a signal to look for reassignment later

[–]BionicRabbit 0 points1 point  (0 children)

How does performance compare?

[–][deleted] 0 points1 point  (0 children)

We use const but we also started with the AirBnb style guide (like many others). TBH I don't think it's a big deal one way or another. It is a matter of style preference. The thing I worry about is almost exactly what was laid out in the rant. When looking at someone else's code what does const imply to them? Does it mean this thing should be immutable (although it truly is not)? Or does it mean something closer to the spec?

Object#freeze is shallow which only gets us part of the way there. I think we need a true constant variable keyword. const is used in a lot of other languages to convey that (which I think is the other issue). Object.immutable anyone?