News for hackers. Stuff that matters.
Re:Airbnb JavaScript Style Guide by elisee on Friday July 03, 2015 @01:10AM (#9823625)
Regarding using single quotes for strings (https://github.com/airbnb/javascript#6.1), I found it interesting that it's one of the rare sections where there's no rationale offered.

I guess it's just a stylistic choice in the end, but when we set up our own internal/informal style guide, my teammate and I spent a little while trying to come up with a justification for single vs double quotes. We ended up choosing double quotes based on the fact that JSON already made that decision for us: it requires strings be double-quoted.

(Although again, it's far from an important matter, as long as you're consistent), anybody has interesting rationales to share in favor of single quotes?

Re:Airbnb JavaScript Style Guide by masklinn on Friday July 03, 2015 @05:13AM (#9824426)
> We ended up choosing double quotes based on the fact that JSON already made that decision for us: it requires strings be double-quoted.

My personal style guide is to copy Erlang: double quotes for text, single quotes for programmatic strings (atoms/symbols). The single quote is slightly more convenient to type on a qwerty keyboard, but text regularly contains single quotes (apostrophes). It also provides a semantic visual shortcut.

Re:Airbnb JavaScript Style Guide by applecore on Friday July 03, 2015 @08:04AM (#9824972)
CoffeeScript follows a more common style: double-quoted strings for interpolated values and single-quoted strings are literal.
Re:Airbnb JavaScript Style Guide by rhgraysonii on Friday July 03, 2015 @10:10PM (#9828859)
Also: Ruby/Opal (also transpiled)
Re:Airbnb JavaScript Style Guide by jaredmcateer on Friday July 03, 2015 @01:38AM (#9823713)
We enforce double quotes for html attributes, mainly because we enforce single quotes for PHP so when you're writing mixed HTML/PHP in the view scripts we don't need to escape any of them. Single quotes for javascript would allow for the same, though we also have a strict no inline javascript policy as well.

Mainly our rationale is, pick one and be consistent. Single quotes where there first so they win, same deal with indentation amount.

Re:Airbnb JavaScript Style Guide by YorkianTones on Friday July 03, 2015 @01:39AM (#9823716)
If you use contractions like don\'t inside of a string then you need to escape the apostrophe if you\'re using single quotes, which is annoying and can make the string a bit harder for humans to parse. I like the aesthetics of double-quotes better too, though that\'s not a compelling reason.
Re:Airbnb JavaScript Style Guide by vog on Friday July 03, 2015 @02:57AM (#9823963)
For such cases I find the Python style most appropriate. It says that you use some consistent default (e.g. always single quote), but for all a strings that require escaping, you should use the delimiter with the least (preferably no) escaping.

Note that this is easier in Python than JavaScript because Python provides more string delimiters:

    'He said hello.'

    'He said "hello".'

    "Don't say hello.'

    '''Don't say "hello".'''

    r'Some regex\sstuff'
Re:Airbnb JavaScript Style Guide by icebraining on Friday July 03, 2015 @06:11AM (#9824601)
The only problem is that sometimes it leads to bugs when people mix different delimiters - like in your third example :)
Re:Airbnb JavaScript Style Guide by jaredmcateer on Friday July 03, 2015 @09:21AM (#9825401)
Note that as of ES6 there is the template string literals using backticks so it looks like JS is at parity with your example.

    'He said hello.'
    'He said "hello".'
    "Don't say hello."
    `Don't say "Hello".`
    /some regex\sstuff/
Mainstream support just isn't there yet.
Re:Airbnb JavaScript Style Guide by elros on Friday July 03, 2015 @02:31AM (#9823877)
Well, one simple typographical way to solve this is not to use a quotation character (') when what is meant is an apostrophe character (’).
Re:Airbnb JavaScript Style Guide by recmo on Friday July 03, 2015 @07:08AM (#9824766)
"Don’t say “Hello”"

Proper typography solves all.

My toy programming language uses “ and ” for string literals. It counts nesting, so you can write “Don’t say “Hello””. There is no escaping. In fact, you can quote any piece of code by simply enclosing it in “ and ”. Code is commented out by turning it into an unused string literal.

Re:Airbnb JavaScript Style Guide by vog on Friday July 03, 2015 @02:51AM (#9823942)
Ouch, I'd be afraid that this may become the source of subtle bugs.
Re:Airbnb JavaScript Style Guide by JimmyM on Friday July 03, 2015 @03:08AM (#9823994)
I thought that (') was the apostrophe character, although it doesn't look like one. (Source: http://www.cl.cam.ac.uk/~mgk25/ucs/apostrophe.html) The same source agrees with you elsewhere though, making my distinction pedantic. (http://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html)

I can't personally imagine this solution would be any less confusing, given that the new preferred apostrophe character according to this source is specifically a right single quote character.

Re:Airbnb JavaScript Style Guide by marcoms on Friday July 03, 2015 @06:02AM (#9824582)
You could use ` backticks instead and use both " and ' in strings without worry
Re:Airbnb JavaScript Style Guide by Bahamut on Friday July 03, 2015 @02:19AM (#9823842)
You can use template strings in ES6 and avoid that whole problem.
Re:Airbnb JavaScript Style Guide by clintonc on Friday July 03, 2015 @01:19AM (#9823656)
My rationale for single quotes is also that JSON chose for you -- if you have JSON-formatted strings in your javascript and you use single quotes for your strings, you don't have to escape the double quotes.
Re:Airbnb JavaScript Style Guide by joepie91_ on Friday July 03, 2015 @06:51AM (#9824719)
I'd find it very worrying that you have JSON-formatted strings in JS code. Why would you do that, when there's also a native representation?
Re:Airbnb JavaScript Style Guide by phpnode on Friday July 03, 2015 @06:33AM (#9824667)
Do you do that more or less frequently than writing strings with apostrophes?
Re:Airbnb JavaScript Style Guide by Void_ on Friday July 03, 2015 @01:31AM (#9823700)
Typing single quotes is one keystroke ('), typing double quotes is two (shift+').
Re:Airbnb JavaScript Style Guide by teamhappy on Friday July 03, 2015 @01:48AM (#9823753)
> Typing single quotes is one keystroke

Not on my keyboard.

Also, C uses double quotes (and JSON, and many, many more languages). That used to be my rationale. These days I get away with saying that 'foo' and `foo` look too similar.

Re:Airbnb JavaScript Style Guide by mixmastamyk on Friday July 03, 2015 @03:45AM (#9824131)
I believe the most commonly ones used do.
Re:Airbnb JavaScript Style Guide by omribahumi on Friday July 03, 2015 @04:57AM (#9824386)
What keyboard locale(?) is that?
Re:Airbnb JavaScript Style Guide by teamhappy on Friday July 03, 2015 @05:49AM (#9824542)
I'm german, but the layout is used in other countries as well: https://en.wikipedia.org/wiki/QWERTZ
Re:Airbnb JavaScript Style Guide by mc808 on Friday July 03, 2015 @03:26AM (#9824048)
I use double quotes for most strings and single quotes for characters, just because it's perfectly valid without having to learn new habits.

If I have HTML in a JavaScript string, I don't quote the attributes at all unless necessary, instead focusing on more pressing matters like how to get that shit out of my JavaScript.

Re:Airbnb JavaScript Style Guide by savanaly on Friday July 03, 2015 @11:20AM (#9826034)
Single quotes are less busy. Strings are all over my angular app usually, so having half the number of little lines flying around is much more pleasing to my eyes.
Re:Airbnb JavaScript Style Guide by adzicg on Friday July 03, 2015 @03:09AM (#9823995)
we use double quotes for HTML and single quotes for JS. that makes it easy to embed HTML snippets in Js code and JS snippets in HTML attribute values without escaping.
Re:Airbnb JavaScript Style Guide by bambax on Friday July 03, 2015 @06:17AM (#9824618)
Single quotes are a big big pain if you have strings with apostrophes (which is common in names: O'Brian, d'Alembert).

You can escape them, but that's an extra pain and strain on readability; you can use some other character but that will usually cause problems down the road.

Why so many people insist on single quotes is a mystery...

Re:Airbnb JavaScript Style Guide by lee on Friday July 03, 2015 @09:59AM (#9825605)
The rationale I have for using single quotes over double quotes in javascript is that when embedding HTML elements, attributes are often quoted in double quotes.

So it's much nicer to write

    '<a href="www.google.com">Google</a>'
than to constantly escape argument strings.
Re:Airbnb JavaScript Style Guide by joshuacc on Friday July 03, 2015 @10:26AM (#9825752)
In ES6, that's no longer a problem since most HTML elements will involve interpolation with template strings and backticks.

    `<a href="${ site.url }">${ site.name }</a>`
Re:Airbnb JavaScript Style Guide by path411 on Monday July 06, 2015 @11:52AM (#9839986)

    "<a href='www.google.com'>Google</a>"
Will work as well though.
Re:Airbnb JavaScript Style Guide by dangle on Friday July 03, 2015 @07:20AM (#9824794)
It looks like they reserve double quotes for JSX attributes:

https://github.com/airbnb/javascript/tree/master/react#quote...

Re:Airbnb JavaScript Style Guide by NhanH on Friday July 03, 2015 @01:18AM (#9823655)
If JSON requires the string to be double-quoted, then it's more convenient to use singe quotes, since your embedded JSON string (if you ever used it) won't need to have its quotes escaped.
Re:Airbnb JavaScript Style Guide by elisee on Friday July 03, 2015 @01:22AM (#9823667)
Interesting. Is that common in your workflow to embed JSON as string literals in JavaScript? Never had to do that. I'd probably use ES6 backticks, so as to have multiline support and skip the need to escape both single and double quotes.
Re:Airbnb JavaScript Style Guide by adrusi on Friday July 03, 2015 @01:52PM (#9826940)
Right, why write

    '{ "foo": "bar" }'
when you could write

    JSON.stringify({ foo: "bar" })
and get a static guarantee of valid JSON?
Re:Airbnb JavaScript Style Guide by _jnc on Friday July 03, 2015 @01:16AM (#9823643)
At least in Ruby, you can only do string interpolation with strings in double quotes.
Re:Airbnb JavaScript Style Guide by elisee on Friday July 03, 2015 @01:18AM (#9823654)
JavaScript up to ES5 doesn't support string interpolation at all. ES6 introduces backticks `before${var}after` for that, but there's still no functional difference between single and double quotes.
Re:Airbnb JavaScript Style Guide by mixmastamyk on Friday July 03, 2015 @03:49AM (#9824145)
Interesting they chose that, considering it's deprecated in bash and python.
Re:Airbnb JavaScript Style Guide by sprobertson on Friday July 03, 2015 @01:35AM (#9823707)
Coffeescript as well. I try to use single quotes whenever possible, so if a string is templated it stands out a bit with the double quotes.