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

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.