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

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?

2

u/eattherichnow 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.

Well, it doesn't. That other invocation? It's actually another const. Because it's in a new scope.

Const isn't just a "safety feature," it's an IDE hint and documentation. Just like type systems, for example.

1

u/pilibitti 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.

That's.... not true. Where did you even learn this definition of const?

A const is something that will never change in the scope that it is defined in. That's it.

If it is a global variable, the scope is the whole application.

If it is a function variable, the scope is the local function.

It makes perfect sense and it prevents errors, and it communicates your intent clearly. What is your issue with it?

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.

Part of good code is communicating intent. For future you and future maintainers. Const is a valuable tool to communicate intent. It isn't just for preventing errors as you are typing the particular method at that moment.

Additionally, using const to refer to arrays and objects is just maddening when you change the contents/structure of those things 7 lines later.

That's abuse.

You have a warped view of identity in code. You can very well mutate the object stored in a const variable. You can't reassign to it. That is the point. If we follow your logic, then even application-wide consts have very little value. How many global and non-object (since objects are a no-no) variables do you need in an application to begin with?

Sorry but you are completely missing the point of const.