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).

→ More replies (10)

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.

→ More replies (5)

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.

→ More replies (3)
→ More replies (2)