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

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.

11

u/a_tocken Apr 02 '18

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

0

u/[deleted] Apr 02 '18

[deleted]

1

u/a_tocken Apr 04 '18

The Google style guide for Java, which is generally pretty sane, recommends braces always because it is harder to mess up. I think this is more true for teams rather than solo users. I favor braces but I will make exceptions to the rule.

-4

u/rorrr Apr 02 '18

You forgot a semicolon at the very end. Remember, after every statement. What a silly rule.

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.

1

u/a_tocken Apr 03 '18

I think you are flat out wrong. This style recommendation is clear to the vast majority of people, and it seems that only people who don't like the semicolon rule are complaining about the silly strawman you're presenting.

Give an example where a brace control block has an ambiguous ending that a semicolon fixes. Give me an example of somewhere where people seriously suggest adding semicolons to the end of control blocks.

-2

u/rorrr Apr 02 '18 edited Apr 02 '18

The rule doesn't say anything about delimiters. It literally says:

Every statement must be terminated with a semicolon.

"if {}" is a statement.

7

u/a_tocken Apr 02 '18

Is your problem with the rule, the wording of the rule, or your inability to understand the rule?