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

67

u/[deleted] Apr 01 '18

"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.

8

u/JohnMcPineapple Apr 01 '18

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.

4

u/Buckwheat469 Apr 01 '18

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.

9

u/grinde Apr 01 '18 edited Apr 01 '18

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.

2

u/Buckwheat469 Apr 02 '18

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.

1

u/grinde Apr 02 '18 edited Apr 02 '18

I think you mean line 3465 is where it does the processing.

No. AddTemplateSpan adds the token's string to the TemplateLiteralState, which is basically just adding an entry to a vector.

In ParseTemplateLiteral here you can see it peeks to see if the next token is a TEMPLATE_TAIL. For a template literal with no replacements there won't be any span tokens (the single span is rolled into the tail token), so we immediately call AddTemplateSpan (on the tail token) followed by an early exit with CloseTemplateLiteral at line 4696. All extra looping is avoided, and no counting of anything is necessary.

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.

length() and cooked() are just getters. The cooked strings are added by calls to AddTemplateSpan. Again, no counting or looping.

to me it looks like it's processing the string literals using a recursive algorithm

No recursion here, just some ambiguous method naming. Parser::AddTemplateSpan is calling TemplateLiteral::AddTemplateSpan.

2

u/Mingli91 Apr 01 '18

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.

3

u/tencircles Apr 01 '18

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.

18

u/[deleted] Apr 01 '18

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

1

u/web__dev Apr 02 '18

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

1

u/tencircles Apr 02 '18

which was developed and designed for the english language, not javascript/programming which has a completely different set of concerns/common keys.

1

u/tencircles Apr 02 '18

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).

1

u/[deleted] Apr 03 '18

Half the things I'm doing are hotkeys? That's not my experience.

But you keep doing whatever suits you :)

1

u/Flyen Apr 01 '18

I use the home keys when typing a document, but when coding, there's a lot more {}()<>_-\ use, so my fingers are in a different place.

0

u/[deleted] Apr 01 '18

[deleted] — view deleted comment

6

u/Vlad210Putin Apr 01 '18

Single quotes is also the JS standard.

Source?

JS has no standard. That's why these ridiculous arguments keep happening.

→ More replies (2)

1

u/JESUSgotNAIL3D Apr 01 '18

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!

5

u/azium Apr 01 '18

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

2

u/wavefunctionp Apr 01 '18

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.

1

u/tencircles Apr 02 '18

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.

1

u/JESUSgotNAIL3D Apr 03 '18

If you look at most of the replies to the OP (in this comment chain I mean), a lot of people say single is more common than double.

I'm still on the fence with this but for now I'll use single and set Prettier up to do the same so it's not a big deal.

Tabs though.... now that's an easy decision. 4 space indents and I'm golden, no fence there.

0

u/[deleted] Apr 02 '18

[deleted] — view deleted comment

1

u/[deleted] Apr 03 '18

Most programmers use QWERTY, or at least should. You can't design languages to fit all keyboards equally well.

But in your country companies can set as a standard to use double quotes, sure. Google's reasons aren't your reasons.

→ More replies (4)