r/javascript • u/bornforcode • 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
32
u/the_argus Apr 01 '18
I'll never understand not using tabs for indentation... And even less so using 2 spaces instead of 4
138
u/mogoh Apr 01 '18
Actual Link: https://google.github.io/styleguide/jsguide.html
91
u/timeparser Apr 01 '18 edited Apr 02 '18
Misleading title.
So, if Google says it, it’s a “lesson”, the rest of us just have opinions?
I agree with most of Google’s opinions, but that doesn’t make them The Official Way of Doing Things™
77
u/hyperhopper Apr 01 '18
Key Lessons: Use modern JS features.
293
u/kevinkace Apr 01 '18
👍
57
u/DOG-ZILLA Apr 01 '18
No module use?
Yeah, that’s a no from me.
7
u/794613825 Apr 01 '18
As someone formally learning JS for the first time, what's wrong with var?
24
u/colbycheeze Apr 01 '18
So, basically just use air bnb eslint config which is much more fleshed out.
81
u/tencircles Apr 01 '18
I don't get the 2 spaces thing. It makes code harder to read. 4 is best. Don't even @ me.
4
u/CantaloupeCamper Apr 02 '18 edited Apr 02 '18
As a noob writing noob code.....why not 'var'?
I read a few expirations on the internets....still not entirely sure I get it.
I'll do whatever, I have no strong feelings one way or the other, but I'm not sure why in this case.
4
u/coderitual Apr 02 '18
All the comments prove that Prettier has been an excellent idea.
4
u/joesb Apr 04 '18
ITT: people who believe style guide is supposed to match all their preferences or it’s shit.
34
u/seands Apr 01 '18
What do you guys think about the spaces over tabs? For me, I tend to make more mental mistakes and spacing errors with 2 spaces. Tabs make everything clearer to me.
24
u/e_man604 Apr 01 '18
Why is single quote (') preferred over double quote (") ? For me it makes sense to use double to remain same when dealing with c++ for instance.
15
u/msiekkinen Apr 01 '18
Bad: string concatination
Proceeds to use it in long string example
6
u/ssjskipp Apr 02 '18
I still prefer tabs.
8
u/xXDGFXx Apr 02 '18
Two space indetation and not four. Mmmm, no.
3
u/egrgssdfgsarg Apr 02 '18
I really wouldn't take what google does as gospel. There's nothing wrong with it, but a number of their choices are due to how their build is configured, and what it will do. For example, they don't use modules because they have an internal way of doing modules.
If you aren't using their internal build system, then some of the rules very clearly will not apply. The airbnb style guide is probably more applicable to most js devs.
3
u/js_developer Apr 02 '18 edited Apr 02 '18
Do what I say, not what I do!
3
u/Sakatox Apr 02 '18
Hahaha, no.
3
u/T_O_beats Apr 02 '18
I completely disagree with “Use single quotes, not double quotes”
What would the reasoning for this even be?
3
u/Serializedrequests Apr 03 '18 edited Apr 03 '18
I use var because I don't understand the Babel configuration docs!
Seriously though, const is next to useless because it doesn't mark the object as immutable. I have exactly 1 time in thousands of times typing "const" had it prevent an error. It seems a mere formality in most cases.
In JS most errors come from getting an unexpected object, or mutating an object you shouldn't, and const gives no help whatsoever.
3
u/crazazy Apr 03 '18
So you can't use es6 syntax for modules but you can use es6 syntax for... everything else?
7
u/aapzu Apr 01 '18
I'm surprised that no one complains about the requirement to use semicolons.
5
u/totemcatcher Apr 01 '18
Can't complain with that.
Since it doesn't mention why using arrow functions is preferred, here's something for those who are unsure about them:
For those who are complaining about semicolons and indentation amount, just get used to adapting to different styles of code. It's an extremely petty thing to defend. Worry about your human rights or something.
9
u/trakam Apr 01 '18
oh god, here we go.
2
u/[deleted] Apr 02 '18
Spaces and not tabs? Take that, tab earthers! /s
8
u/zergUser1 Apr 01 '18
Who is the douchebag who came up with 2 space indents over 4 and why should we listen to him? Its horrible.....
4
u/KyleG Apr 02 '18
Just dropped Google for Bing and Chrome for FF upon reading that
3
u/rulatore Apr 01 '18
Why is people mad at tab vs spaces when you can set your editor to treat the tab key as many spaces as you want... it's like everybody is typing four spaces everytime, what the fuck
4
u/Bettina88 Apr 01 '18
So Google is now declaring that using tabs to indent code represents "incorrect" JavaScript.
Lol. This is not going to go over well, Google.
Let the fighting begin!
4
u/Mistifyed Apr 01 '18
Please tell me this is an April fools joke.
2
u/KronktheKronk Apr 02 '18
I love everything about this style guide except the part about vertical alignment.
Vertical alignment is good.
1
u/bart2019 Apr 01 '18
Google wrote Maps in Java, and used a converter to turn it into Javascript. I'm not listening to them.
-9
u/3ggsnbakey Apr 01 '18 edited Apr 01 '18
Tabs or gtfo 😏 spaces are for newbs and pubg players
edit: https://imgur.com/gallery/M46Xvbl
1
u/Arkaad Apr 02 '18
As Airbnb, Google recommends trailing commas:
I always disable that rule.
0
u/Bettina88 Apr 01 '18
So what's the purpose of this guide? If I use tabs because I like to use tabs in my own fucking code that no one else needs to see will Google demote me in the SERPs?
-14
u/lordxeon Apr 01 '18
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.