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

-14

u/lordxeon Apr 01 '18

Use const by default

I will get downvoted for this, but (like everything) JavaScript developers take a good idea, and destroy it. Function level variables should not be a const. Const variables should be those that never change during the run-time of the application.

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.

→ More replies (2)

4

u/inu-no-policemen Apr 01 '18

Currently it's backasswards. You write your code with let, then your editor tells you that you never reassign anything to it, and provides a quick-fix to change it to const. Or you use const and then there's a quick-fix to change it to let.

It would make more sense if the machine would simply tell you via highlighting if you're reassigning something elsewhere or not. Not only wouldn't you have do anything to get that, you'd even see this information whenever and wherever the variable is used.

→ More replies (1)

6

u/atra-ignis Apr 01 '18

Can you give an example why you think this is bad. I always use const by default. I use immutable.js by default too whenever I can.

I very very rarely use let.

-3

u/lordxeon Apr 01 '18

I think it's bad because it's an abuse of what a const is. A const is something that should never change in the life-cycle of an application, not the life-cycle of a function.

I can count on one hand the number of times I accidentally re-assigned a variable to something I didn't mean for it to go to. (And none of those times did it ever make it out of my local development testing). If you are getting so sloppy that you have to use const to prevent yourself from doing that then you should remove some distractions from your coding life-style.

Additionally, using const to refer to arrays and objects is just maddening when you change the contents/structure of those things 7 lines later. Yes the reference to the array/object hasn't changed, but you changed every single thing inside of it. It's not a const anymore. That's abuse.

3

u/kobbled Apr 01 '18

A const is something that should never change in the life-cycle of an application, not the life-cycle of a function.

As long as it doesn't change in the scope of wherever it's declared, I'm fine with it.

6

u/atra-ignis Apr 01 '18

I think you're maybe misunderstanding the purpose of a const. Something that doesn't change within the lifecycle of a function is perfectly valid. Global constants are valid for config too but why limit a useful tool just because it doesn't fit with your view of what a const should be? I don't need to count the times I've accidently reassigned a const. It's never happened. For your objects and arrays point, knowing that a variable always points to the same object is valuable, even if the contents of that object has totally changed.

If I'm reading someone else's code and it doesn't use const I just sigh because it means I now need to look through the function and see if it's changed anywhere (and it it hasn't I change it to a const so the next person reading the code doesn't need to do that).

Going back to the immutable example, if you had an immutable object scoped only to a function would you declare it as a let?

→ More replies (2)

5

u/[deleted] Apr 01 '18

It's a phase. At one point in Java a sub-group of FP aficionados were obsessed declaring every local variable final, regardless of practical utility. Now people are taking a few steps back and applying more common sense. Java 8+ even permits capture of variables that are effectively final, without them being explicitly declared final.

1

u/slikts Apr 02 '18

The analogy to the Java situation is not very apt, since there's no impracticality in avoiding var; code that only uses const or let is more consistent than code that also uses var, and using const by default makes code more readable, since the reader doesn't have to consider possible reassignment.

2

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

there's no impracticality in avoiding var

I've said nothing about var.

using const by default makes code more readable, since the reader doesn't have to consider possible reassignment.

Be honest: when was the last time your starting point to reading / changing code was "I need to consider which one of these local variables I could reassign"? Has that ever happened to you?

This decision what to reassign doesn't flow from how the variable was declared. It flows from what you want the function to do in the end. If you need to reassign a const variable, you wouldn't even skip a beat, you'd go change const to let. You wouldn't treat pre-existing declarations as eternal and unalterable. Heck, most times when refactoring a local function, many of the pre-existing variables might not even survive the edit at all.

What matters is what you want the code to do, not how it happened to be factored the first time around. And if you're honest, you'd agree with that.

It's very easy to come up with artificial scenarios where const is helpful. They may even pass as logical in a Reddit comment. But if they make no sense while you're coding, if they rarely if ever happen in the real world, you're basically lying to yourself.

And you may consider this a superficial concern, but if const is to be a default choice for local variables then it has the worst name ever. A constant is not a variable. It's the opposite of a variable. A variable "varies", and constant is "constant". We're doing this because we like math and functional programming, right? In math and functional programming, a variable is not reassignable, but it can vary on each function run. A constantly is universal. Like Pi, or E. Pi doesn't change depending on your app runtime state. A constant shouldn't depend on runtime state, it shouldn't be mutable. It makes it a not-constant. It makes it a variable.

When the goal is "make code readable", this is relevant. Words have meanings. When I'm skimming through post nine thousand about how "JS const is not about constants" I don't think "wow I'm so glad I know this, this is opening new doors to me, everything can be const". No, I think "ECMA fucked up".

→ More replies (1)

2

u/[deleted] Apr 01 '18

[removed] — view removed comment

0

u/[deleted] Apr 01 '18

[deleted]

4

u/eattherichnow Apr 01 '18 edited Apr 01 '18

Look, const sounds new, and therefore hype, and a thing being hyped then it's wrong. Frankly, anything liked by more than two people is bad and wrong, and if one of them posted something about it to Reddit in 2009 then they're bad, wrong and can't shut up about it.

Edit: I forgot to mention that I am very smart.

1

u/tencircles Apr 01 '18

var is lexical scope, not global scope.

1

u/slikts Apr 02 '18

Not sure what OP meant, but for the sake of accuracy, var a = 1; outside of function scope will assign to window.a, i.e., the global object, so var is either function or globally scoped, while const or let are never global.

→ More replies (4)
→ More replies (2)