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

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.

25

u/rafalg Apr 01 '18

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.

-4

u/[deleted] Apr 02 '18

[removed] — view removed comment

4

u/a_tocken Apr 02 '18

The braces represent a GROUP of statements. The meaning of "semicolon after every statement" is after every statement that doesn't have an ending delimiter. A control block does have an ending delimiter, so there is no ambiguity.

1

u/[deleted] Apr 02 '18

The rule isn't "use a semicolon after every statement that isn't already terminated by some other ending delimiter", it's "use a semicolon after every statement". There's no exception there for blocks that don't actually need a semicolon according to JavaScript grammar. Semicolons are neither required nor injected after an if statement, but they are required after most block statements.

If you feel compelled to inject meaning into a style rule, then you aren't adhering to the style guide. The whole point of this rule is so that the developer doesn't have to concern themselves with the ins and outs of ASI.

It's worth pointing out that Google doesn't follow this rule as it is written, either.

→ More replies (1)