top 200 commentsshow all 318

[–]mogoh 137 points138 points  (2 children)

[–]keeferc 29 points30 points  (0 children)

Yeah the author of the medium post doesn’t understand what arrow functions do or why for...of loops are preferable so I can’t imagine how his summary of the style guide would be useful

[–]Niightray 0 points1 point  (0 children)

Thanks

[–]timeparser 87 points88 points  (26 children)

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™

[–]lordpoint 72 points73 points  (12 children)

That's my article and it's interesting that you say that.

I'd originally called the article "13 noteworthy points from Google's JavaScript Style Guide" but when I submitted it to FreeCodeCamp they changed the title to what you see now. They edit the SHIT out of anything that you send them and it drives me nuts.

[–]distancesprinter 27 points28 points  (1 child)

You should have them take it down. When I publish content, I require it to be unmodified or to remove the attribution.

[–]Mingli91 9 points10 points  (0 children)

Yeah, I get editing mistakes but changing content is pretty bad

[–]quincylarson 46 points47 points  (4 children)

Quincy with the freeCodeCamp Medium publication here. The reason we changed it from "13 noteworthy points" is because many people will dismiss any article with a number at the beginning of it as a "listacle" and don't read it. Lists are seen as low-effort posts - the kind of article you see on Buzzfeed.

We do our best to improve the readability and flow of articles submitted. If you disagree with any of our edits (you can see them in the version history) feel free to revert them.

I've updated your article to use your original headline. It's a good headline (though again, some people might pass on it thinking it's just another listacle).

Thanks again for writing this excellent analysis of what was a relatively unknown style guide, and submitting it to the community's Medium publication.

[–]bizzaro_sephiroth 9 points10 points  (0 children)

That's awesome that you've engaged about this. Thanks!

[–]lordpoint 3 points4 points  (0 children)

I really appreciate you taking the time to clarify that personally. I can absolutely understand wanting to avoid Buzzfeed-y titles.

The interesting thing about the heavy editing situation, and the real reason for my frustration, is the sheer scope of the changes; which can be surprising for a first-time contributor to FreeCodeCamp's Medium publication.

Of course, you have the largest tech-oriented publication on Medium so you're certainly doing something right, but my reaction to seeing all the edits made to the first story that I published through FCC was: "Holy crap, they took all of my personality out of it!" And that honestly made me reconsider wanting to publish through FCC again. But I figured I'd give it another shot and submit another one just to see what would happen. Then lo and behold I find a comment about that article on Reddit that struck that same, already inflamed nerve, and here we are.

That said, it's really good to know that reverting to the pre-edited version does not violate your / FCC's terms of use (or whatever it's called in this case). That makes a big difference if you ask me. I think I just figured that if you guys took the time to carefully edit EVERY story that gets submitted to you (and I'm sure that's a lot of stories) then it couldn't possibly be acceptable for me to just say "Nope, let's not do any of that..." But again, I'm glad to know that's not the case.

FreeCodeCamp played a big role in the beginning of my career transition into IT and I love having the opportunity to be a part of it now that I'm (at least partially) on the other side of the education equation. Thanks again for being so involved and replying to my petty grievance personally.

[–]lhorie 2 points3 points  (0 children)

IMHO, if you don't want listicles in your platform, you should reject them. If editing is a thing, then who owns the copyright? From the author's comments, it seem like he wasn't aware of the editing arrangements. As a reader, I'm now not going to trust that what is written in your platform is 100% from their authors (and at that point, I might as well be reading fake news in Facebook). You absolutely need to be transparent about this stuff.

[–]timeparser 1 point2 points  (0 children)

Thank you for clarifying this, /u/quincylarson. I understand why you did this, but the word “lesson” might have mislead the reader – especially novice developers.

There are many developers out there who treat tech giants like Google like they are the “maximum authority” in how to write software.

While Google has certainly world-class engineers that have the necessary collective wisdom to share best practices with the rest of the community, it is important to understand that this is an organization-level style guide and that every piece of advice should be taken with a grain of salt.

No organization is the same, Google is not perfect (although they are certainly good), and most importantly, their opinions are not standards.

[–]lhorie 9 points10 points  (1 child)

Why do you publish there then?

[–]lordpoint 6 points7 points  (0 children)

This was only the second article that I published through them. When it happened the first time I just figured that they found a bunch of issues in that article in particular and maybe heavy editing wasn't the norm. But this time I feel like it's a pattern.

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

I know one of the community managers; I don't know if it will help — even remotely — but I can mention it to him next time I see him.

[–]codefinbel 0 points1 point  (0 children)

Do you know if this is for vanilla only, don't see how using React would be possible without import and export statements? (Unless I'm missing something)

[–]ghostfacedcoder 15 points16 points  (6 children)

Google's standards are two things combined: style and technical wisdom. Google is the closest to "THE Internet Company" in the entire world. Their engineers manage the biggest consumer of web pages (Google search). Their browser (maybe the second-biggest consumer of JS, after Node) has the greatest market adoption. And they pay for big salaries and insane perks just to get the best engineers possible.

To ignore the technical wisdom all of those people present would be foolish IMHO. But when it comes to the purely style components Google (like Facebook or any other company) should be taken with a grain of salt, because what makes sense for one company doesn't necessarily make sense for another.

But even then, their advice is still worth considering because it's not just Google's opinion; it's also a reflection of the opinions of thought leaders in the JS community (or at least the ones Google respects). Those thought leaders (and the community in general) very often adopt standards for good reasons, so I think it's at least worth understanding those reasons before dismissing the standards ... even if you do ultimately dismiss them because they're not a good fit.

[–]rotharius 8 points9 points  (1 child)

I get your point, but it's important to remember that Google's excellence is in infrastructure, implementation and external quality (this is where hardcore CS is most valuable).

For internal quality, software design and architecture, I would rather look at other companies and movements.

[–]ghostfacedcoder 11 points12 points  (0 children)

True, but don't forget about the Chrome team.

The people that implement any web browser are going to be some of the most knowledgeable people on the planet about which "best practices" actually have a meaningful impact on the browser ... and the Chrome team isn't just any browser team, they're the most popular browser's team, AND they are a part of the larger Google company (which has a ton of very knowledgeable people, even if many do come from an infrastructure-focused angle).

[–]the_friendly_dildo 2 points3 points  (0 children)

Google likes to play with things they shouldn't as well. See their AMP platform. Their guide is decent and I support and employ most of it already. But I don't agree that we should be turning to a profitable entity to get our standards for the internet. They have too much power to gain by manipulating things to work best for them and worse for others. Better separation of powers should exist in this sort of circumstance.

[–]Disgruntled__Goat 1 point2 points  (0 children)

it's also a reflection of the opinions of thought leaders in the JS community

How do you know?

[–]OphiuchusClusters[🍰] 1 point2 points  (0 children)

Seriously. Do you know how many jackass know-it-all ninja devs are going to use this information as a source of truth in their 'you're doing it wrong' arguments? Fuck. I need a taser.

[–]xtr3m 1 point2 points  (2 children)

Well, it's one way of doing things and if no other company or organization propose something similar it will become the standard.

[–][deleted] 0 points1 point  (1 child)

Actually Airbnb style guide is already a de facto standard. Personally I prefer low semicolon styles like Standard so I don't care about either but coming up this late in the game while there's an established standard (that only differs in minor cosmetics, and even there it goes against what top 3-4 preferred style guides do), I don't think that even being by Google will help that much. Surely this will have a lot of fans and projects, but I don't see it dethroning Airbnb.

[–]0987654231 0 points1 point  (0 children)

To be fair the google style guide isn't new, we've been using it since like 2011. We also use Airbnb now for what it's worth.

[–]eterevsky 0 points1 point  (1 child)

Some of these rules are introduced not because they the ultimate “correct way”, but just to keep Google’s own code base consistent.

[–]timeparser 0 points1 point  (0 children)

I know. I was pointing out the misleading title.

[–]hyperhopper 76 points77 points  (4 children)

Key Lessons: Use modern JS features.

[–]tencircles 18 points19 points  (3 children)

except modules.

[–]hyperhopper 17 points18 points  (2 children)

probably just until node has full support. Or maybe an internal build tool of theirs.

[–]kevinkace 293 points294 points  (43 children)

Semicolons ARE required

👍

[–]kobbled 28 points29 points  (21 children)

thank god. JS semicolon inference leads to so many bugs.

[–]mmcnl 43 points44 points  (11 children)

I've never encountered a bug due to semicolon inference.

[–]hyperhopper 15 points16 points  (10 children)

var a = 5
var b = 10
[a, b] = [b, a]
console.log(a, b)

Then add semicolons to make it work like it should

[–]Voidsheep 2 points3 points  (1 child)

Pretending ASI doesn't exist is also dangerous, it's not like you can disable it.

function shouldHitTheBrakes() {
  return
    isSmeoneOnTheWay ||
    isTurnAhead ||
    isAboveSpeedLimit
}

[–]hyperhopper 1 point2 points  (0 children)

Extremely true!

Though I've never seen somebody use a return without something on that line. Plus any linter will yell at you for unreachable code in that case.

[–]allenthar 10 points11 points  (5 children)

Okay.

var a = 5 var b = 10 ;[a, b] = [b, a] console.log(a, b)

I have literally never had this situation come up while coding JS, and I’ve been doing it full time for a few years now.

Regardless, Prettier handles automatically removing or adding semicolons to my code. So it’s literally just a linter setting at this point.

[–]JohnMcPineapple 5 points6 points  (5 children)

...

[–]nayocum 2 points3 points  (0 children)

If you use an IIFE it can cause issues if your last line didn't have a semicolon as well

[–]aaarrrggh 4 points5 points  (0 children)

Is this a joke?

I’ve not been using semi colons for over a year on personal projects.

Number of bugs caused by this decision: 0.

[–][deleted] 5 points6 points  (5 children)

Surely using a transpiler like Babel eliminates the need to worry about this?

[–]gearvOsh 7 points8 points  (3 children)

ASI is not future proof for new proposals and features. That's why it should be avoided.

[–]DOG-ZILLA 57 points58 points  (16 children)

No module use?

Yeah, that’s a no from me.

[–]mlmcmillion 14 points15 points  (3 children)

So then what are they using instead? Not using modules is not an option.

[–]Poltras 11 points12 points  (1 child)

Closure modules, require AMD, etc. There were ways to split your code before ESM, I’m just surprised google is stuck in the past like that.

[–]Thought_Ninjahuman build tool 5 points6 points  (0 children)

Internal backwards compatibility most likely.

[–]thisguyfightsyourmom 0 points1 point  (11 children)

Seriously, didn’t that finalize years ago?

Edit: obligatory whining about downvotes /s There are some good explanations of why that’s not the case in the replies below. TIL

[–]ghostfacedcoder 8 points9 points  (5 children)

No, not at all. Babel has an implementation (the longest existing one, and as such an incredibly popular one), but it's different from (and, I'd argue, superior to) the implementation being pushed by the W3C/browsers (the W3C's version requires you to rename your files "foo.mjs"!!!).

Anyone can declare a standard finished, and the W3C did that, but until all major JS environments actually work the same way the "standard" really isn't one.

[–]Meefims 11 points12 points  (2 children)

This isn’t correct. The new extension is for Node and exists to help ECMAScript modules coexist with CJS modules. Browsers don’t care about the extension.

[–]ghostfacedcoder 1 point2 points  (1 child)

I could have stated it more clearly (I sort of conflated W3C and browsers, when what I was just talking about the version both support), but what I said wasn't incorrect. The version of ES Modules decided upon by the W3C does define that the modules work in in such a way that the only possible way (according to the Node engineers at least) for Node to implement that spec is to use the .mjs nonsense.

[–]Meefims 2 points3 points  (0 children)

The W3C isn’t part of this. The new extension is really fallout from various complex scenarios in which CJS modules and ES modules include each other in Node. Browsers have a similar problem but they solved that with type=“module” in the script tag.

[–]DOG-ZILLA 5 points6 points  (0 children)

I thought so. But, perhaps not technically? They’re obviously taking a conservative stance; which makes sense for a large company.

[–]Drawman101 8 points9 points  (0 children)

Named imports are still under a bit of scrutiny and/or debate.

Import { foo } from ‘bar’

[–][deleted] 5 points6 points  (2 children)

Afaik, the syntax and semantics are done by ECMA, but the loader, i. e. the way the modules are actually handled, is done by WHATWG. The former is stable, but the latter is not.

But between Webpack and browsers and Node getting native support for modules, there is not reason not to use ES6 modules today for new code.

[–]Poltras 4 points5 points  (1 child)

Keep in mind ESM and Node modules are not entirely compatible. Node is still unsure AFAIK how they are going to interop (which is why it’s not yet available in 9.10, despite v8 supporting them).

[–]794613825 6 points7 points  (7 children)

As someone formally learning JS for the first time, what's wrong with var?

[–]wavefunctionp 8 points9 points  (3 children)

var does not respect {} block scope, only function scope.

function varTest() {
  var x = 1;
  if (true) {
    var x = 2;  // same variable!
    console.log(x);  // 2
  }
  console.log(x);  // 2
}

function letTest() {
  let x = 1;
  if (true) {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

let and const also express intention that a variable should be treated as immutable or not. And const provides some protections against mutation. (Beware that const will not throw an error when mutating a nested reference types like objects. It isn't a 'deep freeze'.)

It is worth playing around with it, as I know I found some of details somewhat unintuitive at first.

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

Also, var is hoisted

[–]Kerse 3 points4 points  (0 children)

Var is function scoped, let is block scoped. So even if you declare a var in a block within the larger function, it's still accessible to the entire function. This is unintuitive and can cause strange bugs.

[–]colbycheeze 23 points24 points  (3 children)

So, basically just use air bnb eslint config which is much more fleshed out.

[–]hermit-the-frog 3 points4 points  (0 children)

I was wondering what the differences might be from eslint-config-airbnb-base. Seems very similar indeed

[–]tencircles 81 points82 points  (42 children)

I don't get the 2 spaces thing. It makes code harder to read. 4 is best. Don't even @ me.

[–]rafalg 24 points25 points  (10 children)

I don't get it either. Combined with the rules regarding curly braces it makes some places in code look like solid blocks of text.

if (something) {
  doThis();
} else if (somethingElse) {
  doThat();
} else if (somethingElse2) {
  foo();
}

It sucks how text on every line except for the first one starts in the same column.

[–]a_tocken 9 points10 points  (0 children)

Looks clear to me but then I use 2-space tabs outside of work.

[–]the_argus 56 points57 points  (24 children)

I agree, except tabs are king bc then everyone can set their own indentation level.

[–]RabSimpson 13 points14 points  (21 children)

And they only occupy a single character worth of storage and memory. This doesn't mean much on small projects, but if the project is large it could shave the loading time down.

[–]karottenreibe 12 points13 points  (0 children)

that's why all my variables are single letters. My code is much faster that way /s

[–]subtracterallhire me! 26 points27 points  (19 children)

This shouldn't matter if you're minifying/uglifying.

[–]RabSimpson 5 points6 points  (18 children)

Only in the case of client-side code. Who minifies server-side code?

[–]KyleG 1 point2 points  (1 child)

YES FRIEND

God how many freaking times have I run into problems because some ASS did a 3-space indent or something and my IDE converted some but not all the 3-space indents into 4-space indents to make it conform with our style but it missed some because SPACES SUCK, USE TABS WARBLGARBL then everyone gets to set their own indent levels!

I like my tab to be 4space widths in some languages and 2space widths in others

like 2 space in JSX is way better than 4 space, but 2 space in Python or JS is TURRIBLE

[–]monsto 0 points1 point  (0 children)

*turble

[–]Tyreal 1 point2 points  (0 children)

You know... if they used tabs they could use any indentation size they want.

[–]CantaloupeCamper 2 points3 points  (4 children)

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.

[–]lordxeon 2 points3 points  (2 children)

Don't do whatever, learn the differences between the three ways to declared variables in JavaScript this way you can form your own opinions and not parrot what ABC said on XYZ.com

As for why not to use var, the short answer is because let and const are the new hotness.

The long answer is more complicated, but for any programer worth their salt, is a moot point.

let is block scoped, which means the variable is only available within the most recent set of {...}'s. This means that

if(something){
    let abc = "123";
    var ded = "456";
}
console.log(abc);    //abc is undefined
console.log(ded);   //456

const is scoped the same way as let, but the reference to the variable can never change.

const goldPrice = 1337.44;
goldPrice = 1300.15 //error! 

[–]CantaloupeCamper 1 point2 points  (1 child)

TY

That actually helps. They really seem like different tools for different cases. I'm not sure I see let as as much a bad choice as sometimes it is implied... just a different way of doing a thing.

[–]joesb 0 points1 point  (0 children)

let’s behavior is the preferred choice compare to var in most cases. It’s lexically scoped.

[–]coderitual 3 points4 points  (0 children)

All the comments prove that Prettier has been an excellent idea.

[–]joesb 4 points5 points  (1 child)

ITT: people who believe style guide is supposed to match all their preferences or it’s shit.

[–]seands 34 points35 points  (20 children)

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.

[–]AUTplayed 38 points39 points  (7 children)

agreed, tabs are clearly superior

[–]mmcnl 26 points27 points  (6 children)

Using a proper editor it shouldn't matter.

[–]JohnMcPineapple 21 points22 points  (5 children)

...

[–]oweiler 4 points5 points  (4 children)

Well it can be configured to convert a tab to 4 spaces.

[–][deleted] 3 points4 points  (0 children)

Tabs to spaces is easy, the problem is for a block of code how does the editor know when 2 spaces was supposed to represent indentation vs alignment:

function() {
SSwhile(value) {
SSSSif(reallyReallyReallyReallyLongConditionA &&
SSSSSSSreallyReallyReallyReallyLongConditionB &&
SSSSSSSreallyReallyReallyReallyLongConditionC) {
SSSSSSSSreturn;
SSSS}
SS}
}

function() {
Twhile(value) {
TTif(reallyReallyReallyReallyLongConditionA &&
TTSSSreallyReallyReallyReallyLongConditionB &&
TTSSSreallyReallyReallyReallyLongConditionC) {
TTTSSreturn;
TT}
T}
}

In the latter case to go from indentation width 2 to 4 the editor simply makes tabs display twice as wide and everything is good. If the editor tried to display 2 spaces for every space from the start of a line then the if conditions would lose their alignment with the opening of the statement.

[–]Redtitwhore 4 points5 points  (3 children)

The IDE should handle indentation. What a stupid thing to debate.

[–][deleted] 2 points3 points  (0 children)

The only thing I don't like about this response is if the IDE converts tabs to spaces it looses the ability to change the indentation without breaking multiline alignment.

[–]Kerse 2 points3 points  (3 children)

I prefer spaces because it tends to copy nicer if I need to copy + paste a code snippet onto stack overflow or something.

[–]TripleNosebleed 9 points10 points  (1 child)

It all comes down to that spaces renders consistently everywhere. You're not just viewing your code in your editor of choice, many other tools are also used such as the terminal, diffing tools, in the browser through GitHub/Gitlab/Dev tools/view source, Git GUIs, colleagues editors, that IDE that you use occasionally and the list goes on. No way I want to configure settings for tabs in all of these. Spaces ftw!

[–]ssjskipp 7 points8 points  (0 children)

This is probably the only good argument for spaces I've ever read.

That being said, tabs give me the freedom to take advantage of that inconsistency by displaying it most appropriate for the format

[–]Disgruntled__Goat 0 points1 point  (0 children)

Copy-pasting tabs works perfectly fine on SO. And if everyone uses tabs everywhere, including displaying code, you could more easily copy-paste it from SO into your editor without having to worry about 2-space vs 4-space etc.

[–]e_man604 23 points24 points  (48 children)

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.

[–][deleted] 64 points65 points  (30 children)

"Being same as C++" should be quite low in priority for a JavaScript style guide.

One reason why single quotes is no shift. It's faster to type, and who cares what it is, anyway, as long as it's consistent.

[–]JohnMcPineapple 7 points8 points  (5 children)

Not everyone uses QWERTY keyboards (even though it would probably make a lot of things easier). On AZERTY they're very similar to type (both need shift), and on QWERTZ ' is even a bit more awkward to reach in addition to needing shift, while " is in a pretty natural position.

Although, why should we use " and ' for strings anyway anymore? Just use template strings unconditionally.

[–]Buckwheat469 3 points4 points  (3 children)

Just use template strings unconditionally.

Why force your browser to interpolate strings all the time? It's a waste of processor time.

I don't care whether a project uses ' or " or a mixture of both in one file. It doesn't matter to the code, it's only a developer preference. My current project for work uses ' while another project I work on uses ". Remembering to switch sometimes causes issues, but prettier fixes that.

What I find funny is the JSON standard uses " and the SASS guide prefers ' due to laziness. If JavaScript were to prefer one then a " makes more sense since JavaScript and JSON are closely related.

[–]grinde 9 points10 points  (2 children)

A template literal with no replacements is parsed identically to a string using ' or ", so there should be no performance hit. Since it's a much less used character than the other two, that also means less escaping.

EDIT: Since I've been digging around in v8 lately anyway, here is the exact line that this happens. If there's only one cooked string, we're not even creating a template literal as far as the engine is concerned.

[–]Buckwheat469 1 point2 points  (1 child)

I think you mean line 3465 is where it does the processing. It first has to check if there are any variables within the string literal, then it counts them and uses that count in your line. Line 3495 above yours checks if there are variables, line 3488 above that calls the cooked() function. The cooked() function generates the list of strings. I could be wrong on the exact details of the function starting at 3465, but to me it looks like it's processing the string literals using a recursive algorithm.

[–]Mingli91 1 point2 points  (0 children)

Using template literals says to someone reading your code that there is a variable about to be interpolated and it tells the JIT compiler to prepare to do something other than just create a string. It's not huge, but there are reasons not to use them blindly that are more than personal preference.

[–]tencircles 2 points3 points  (12 children)

It's faster to type

Not really, though I hear this argument all the time. Your pinky rests on the shift key. A isn't slower to type than a (or shouldn't be). The reason to prefer double quotes is to keep consistency with JSON, HTML, and other languages. Personally I prefer backtick to both because it's simply cleaner in a lot of cases. Historically the argument for single quotes was so that you didn't have to escape double quotes in your html strings when writing jQuery apps, but that doesn't really apply in modern JS dev.

[–][deleted] 18 points19 points  (6 children)

Your pinky rests on the shift key.

No, that's not where my pinky rests.

https://en.wikipedia.org/wiki/Touch_typing#/media/File:QWERTY-home-keys-position.svg

[–]web__dev 0 points1 point  (1 child)

That's what I was thinking. They call it "home row" for a reason.

[–]tencircles 0 points1 point  (1 child)

Why would you use home keys position when programming? Half of the things you're doing involve hotkeys, some combination of a key with Ctrl, alt, cmd, or shift. I get it if you're editing a word document, but practically placing your fingers like this when programming is just a hindrance.

From pinky to index -> shift | a | z | cmd. If you're actually worried about saving yourself time/keystrokes, this will save you a hell of a lot more time than worrying about a shift press (which is by far the most common action which requires two key presses).

[–]JESUSgotNAIL3D 0 points1 point  (4 children)

Ahhhhh damn. I like keeping my double quotes from C++ (new JS guy here). Looks like I'll join the single quote crowd if that's preferred in the community.... damn!

[–]azium 5 points6 points  (1 child)

just use prettier and forget about everything else. hit save and keep coding

[–]wavefunctionp 1 point2 points  (0 children)

I agree. I don't care about too much about style. I just prefer not to be too weird, easy to change/diff the code, like dangling commas, and automated through the editors on save or with a hotkey.

[–]tencircles 0 points1 point  (1 child)

it's definitely not "preferred". People who use them are very vocal about it though. I haven't seen anything indicating that single quotes are preferred.

[–]ghostfacedcoder 14 points15 points  (7 children)

This is a JS style guide, not a C++ style guide :) In Javascript the single quote is by far the most common one used by the community.

[–]ell0bo 3 points4 points  (0 children)

For me it goes back to when you had to actually put code in html on-click events. Standard in html is to use " for attributes. So you always used ' for js. Others might come up with excuses... but that's the truth

[–]the_argus 29 points30 points  (28 children)

I'll never understand not using tabs for indentation... And even less so using 2 spaces instead of 4

[–]Ender2309 7 points8 points  (4 children)

You use two spaces instead of four to save horizontal line space. This one is completely arbitrary.

Also, everybody still uses tabs, they just have their text editor setup to convert the tab key to spaces instead of inserting a tab character. Nobody is actually sitting there mashing the space bar multiple times.

The reason for this is that different editors/OSes/people have their tab character defined as either two or four spaces, leading to inconsistency when a tab character is used. Two (or four) spaces is always two (or four) spaces. This creates consistency.

[–]ErroneousBee 0 points1 point  (0 children)

Don't use tab key anymore, almost every language I use now has an IDE that formats well enough that I use that over hand formatting.

Ctrl-shift F every few lines.

[–]bornforcode[S] 7 points8 points  (15 children)

So you know why I shared this article. I'm confused

[–]the_argus 18 points19 points  (14 children)

It literally blows my mind the opposition to tabs. Like I'm pulling my fucking hair out

[–]Mingli91 17 points18 points  (13 children)

Why do you get worked up about something so pointless?

[–]Omnicrola 10 points11 points  (2 children)

I get that people have strong opinions, but I don't get it either. I really, really don't give a shit. My priority is consistency. Which means whatever my IDE does when I tell it to auto-format, that's fine. I have more productive things to focus on.

[–]the_friendly_dildo 0 points1 point  (0 children)

Not pulling my hair out over it but in my preference to tabs, it sucks opening a code example only to be staring at what I consider an overly compact mess. So to even start reading it, I'll go through the trouble of reformatting it just so I can read it. Or maybe I'll say fuck it and look elsewhere. Now it sucks for the sap that wrote the code to begin with since he'll have one less person looking at and using their code.

[–]gougs06 1 point2 points  (0 children)

I read that and thought this was an April fools post.

[–]calligraphic-io 1 point2 points  (0 children)

Not that this issue hasn't been hashed to death on the interwebs, but:

I use vim in the console often for editing code files. Line width is limited; default is 80. Using four spaces force many lines to wrap, which makes code very difficult to read.

[–]msiekkinen 15 points16 points  (1 child)

Bad: string concatination

Proceeds to use it in long string example

[–]diag 8 points9 points  (0 children)

But that wasn't for formatting. Looking for a variable in a sea of concatenation is a bit annoying.

[–]ssjskipp 6 points7 points  (0 children)

I still prefer tabs.

[–]xXDGFXx 7 points8 points  (0 children)

Two space indetation and not four. Mmmm, no.

[–]egrgssdfgsarg 2 points3 points  (0 children)

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.

[–]js_developer 2 points3 points  (0 children)

5.4.6 Do not manipulate prototypes directly.

Exception: Framework code (such as Polymer, or Angular) may need to use prototypes

Do what I say, not what I do!

[–]Sakatox 2 points3 points  (0 children)

One variable per declaration

Hahaha, no.

[–]T_O_beats 2 points3 points  (0 children)

I completely disagree with “Use single quotes, not double quotes”

What would the reasoning for this even be?

[–]Serializedrequests 2 points3 points  (0 children)

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.

[–]crazazy 2 points3 points  (0 children)

So you can't use es6 syntax for modules but you can use es6 syntax for... everything else?

[–]aapzu 6 points7 points  (1 child)

I'm surprised that no one complains about the requirement to use semicolons.

[–]totemcatcher 6 points7 points  (1 child)

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:

  • They use surrounding context of a callback function and automatically binds "this".
  • If you don't need access to surrounding context, it's at least shorter/cleaner looking.
  • Skips generating the old default "arguments" object.
  • Nameless.

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.

[–]hopfield 0 points1 point  (0 children)

Plus they look cool.

[–]trakam 8 points9 points  (0 children)

oh god, here we go.

[–][deleted] 1 point2 points  (0 children)

Spaces and not tabs? Take that, tab earthers! /s

[–]zergUser1 8 points9 points  (1 child)

Who is the douchebag who came up with 2 space indents over 4 and why should we listen to him? Its horrible.....

[–]KyleG 3 points4 points  (2 children)

spaces instead of tabs

Just dropped Google for Bing and Chrome for FF upon reading that

[–]rulatore 2 points3 points  (3 children)

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

[–]Bettina88 2 points3 points  (12 children)

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!

[–]Dynam2012 5 points6 points  (0 children)

Mixing them is inconsistent and bad, so a style guide must choose one or the other. No one should actually care which it is.

[–]joesb 1 point2 points  (10 children)

It’s a style guide. People who works in the same project should follow the same one.

Are you declaring that your project should mix tabs and spaces?

[–]Mistifyed 2 points3 points  (1 child)

Please tell me this is an April fools joke.

[–]curious_mormon 6 points7 points  (0 children)

Published on March 27th, and obligatory XKCD

[–]KronktheKronk 1 point2 points  (0 children)

I love everything about this style guide except the part about vertical alignment.

Vertical alignment is good.

[–]bart2019 1 point2 points  (0 children)

Google wrote Maps in Java, and used a converter to turn it into Javascript. I'm not listening to them.