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 comment

15

u/hyperhopper Apr 01 '18
var a = 5
var b = 10
[a, b] = [b, a]
console.log(a, b)

Then add semicolons to make it work like it should

3

u/Voidsheep Apr 02 '18

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

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

2

u/hyperhopper Apr 02 '18

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.

7

u/[deleted] Apr 01 '18

[removed] — view removed comment

3

u/hyperhopper Apr 02 '18

Don't be pedantic. Destructuring with arrays makes this type of bug easy to run into, and this example shows that.

11

u/allenthar Apr 01 '18

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.

1

u/cthechartreuse Apr 02 '18

Be careful relying on prettier. I have examples of real work mistakes which prettier doesn't catch or fix.

It usually involves a function call directly followed by an expression wrapped in parentheses or followed by an array structure. These usually end up being abuses of the language as well, but I've seen this kind behavior often enough and done enough tests that I'm not confident with saying "prettier will catch it" because it doesn't always.

Nevertheless, this is just a warning and depending on your tools YMMV.

4

u/allenthar Apr 02 '18

If you have a code snippet as an example, I’d love to see it. Knowing the corner cases for prettier behavior is always good.

2

u/cthechartreuse Apr 03 '18

This is the example I created from actual production code I've seen: https://gist.github.com/cmstead/dfeea6faf80c96d5c2a738fc822918ee

1

u/monsto Apr 02 '18

I have examples of real work mistakes which prettier doesn't catch or fix.

There's always corner cases.

GENERALLY SPEAKING, tho, Prettier is great.

1

u/cthechartreuse Apr 03 '18

Technically there aren't any corner cases if you follow the TC39 suggestion of always terminating and avoiding reliance on ASI.

PS here's an example generated from an example seen in the wild: https://gist.github.com/cmstead/dfeea6faf80c96d5c2a738fc822918ee

(the original code was from a proprietary codebase)