We should probably have some guidance. One suggestion is:
PREFER Consistency with single versus double quotes.
I think that's a little too vague. Consistent with what? What would be better to say?
Consistent with – everything else? We have a lint for single quotes, so a lot of our code has been drifting that way.
Perhaps we should say something like
1) Prefer using one type of quote in a given file/library/package
2) Prefer using single quotes, to match the preference of the ecosystem?
Why not: PREFER using single quotes.
Although there are certainly edge cases: e.g., "It's ok" vs. 'It\'s ok'.
Fwiw, in our docs and examples we've tried to use ' for Dart code and " for HTML code. We tend to use ' for YAML code (e.g. pubspecs in stagehand templates), but the docs aren't as consistent there.
Why not: PREFER using single quotes.
I don't know if that is the best preference. (Personally, I lean towards double quotes because I've been bitten by contractions in strings too often, and I'm used to other languages that only use double quotes.)
...and JSON only supports double-quotes, so conversion is harder.
The contraction thing drives me nuts, too. But I think Flutter expressed a
preference early and the ship (and the associated lint) has sailed.
On Fri, May 11, 2018 at 3:28 PM Bob Nystrom notifications@github.com
wrote:
Why not: PREFER using single quotes.
I don't know if that is the best preference. (Personally, I lean
towards double quotes because I've been bitten by contractions in strings
too often, and I'm used to other languages that only use double quotes.)—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/site-www/issues/859#issuecomment-388501128,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCigo7iscOm5sKKhXtAhWvfUhm3vlrks5txhCTgaJpZM4T5GaP
.
...PREFER is awesome IMHO
On Fri, May 11, 2018 at 3:33 PM Kevin Moore kevmoo@google.com wrote:
...and JSON only supports double-quotes, so conversion is harder.
The contraction thing drives me nuts, too. But I think Flutter expressed a
preference early and the ship (and the associated lint) has sailed.On Fri, May 11, 2018 at 3:28 PM Bob Nystrom notifications@github.com
wrote:Why not: PREFER using single quotes.
I don't know if that is the best preference. (Personally, I lean
towards double quotes because I've been bitten by contractions in strings
too often, and I'm used to other languages that only use double quotes.)—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/site-www/issues/859#issuecomment-388501128,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCigo7iscOm5sKKhXtAhWvfUhm3vlrks5txhCTgaJpZM4T5GaP
.
This is the same issue as in Python, and I like what the PEP8 says about single vs double quotes:
Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.
Personally I think that typing a single quote, which require using only one key, vs double quotes requiring [shift + 2] (on my keyboard layout at least) also makes it slightly more effective.
@munificent what do you think about @cseder's suggestion? I'm thinking we might say something like this:
DO use single or double quotation marks consistently.
Most of our examples use single quotation marks for Dart code, except when a string contains an apostrophe. There's even a lint to help you consistently use single quotes: prefer_single_quotes
. However, you should match the style of the source code file. If your organization prefers double quotes, use those.
Then we could have examples taken from our code, with single quoted stuff and "It's ok" in the GOOD example, and inconsistent quotes in the BAD example.
What do you think?
I'm somewhat worried that this will just cause people to ask what "consistent" means. I'm also confused by that paragraph because it reads to me like the first two sentences conflict with the last two. If I use single quotes for everything except for strings that contain apostrophes, is that still considered "consistent"?
Also, before we pin anything down, I feel like we should try to get a sense of what the general consensus is, at least internally if not externally too. I'd hate to add a rule that turns out to be in conflict with what many users are doing in their code. I don't want to punish people for invented sins.
I think what it all should boil down to is:
PREFER Single Quotes.
Use double quotes only if it makes your code more readable.
For me personally, I have a Scandinavian layout on my MacBook keyboard, and to type a single-quote I use only one key (the top-left key), but to type a double-quote I have to use Shift + 2, so I PREFER using single quotes everywhere I can! 😅
Better yet: PREFER single quotes if there are no established rules.
Company and project policies should always trump that.
Use double quotes only if it makes your code more readable.
The relies on a consensus definition of "more readable", but the whole point of "Effective Dart" is to be that definition, so this is circular. A rule like this will just cause as much argument as it intends to resolve by getting people to disagree about what's "readable".
We could be prescriptive here: if the string contains singe-quotes that
otherwise would need to be escaped...right?
On Thu, Mar 14, 2019 at 2:15 PM Bob Nystrom notifications@github.com
wrote:
Use double quotes only if it makes your code more readable.
The relies on a consensus definition of "more readable", but the whole
point of "Effective Dart" is to be that definition, so this is
circular. A rule like this will just cause as much argument as it intends
to resolve by getting people to disagree about what's "readable".—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/site-www/issues/859#issuecomment-473066203,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCigFGG8XdG-lasdF4VE6P-msHQjNiks5vWrwKgaJpZM4T5GaP
.
We could say that, yes, but then that leads to:
takesManyStrings(
"this first string's got an apostrophe",
'this does not',
"this's got one",
'not this',
"this one's got \"both\" kinds");
Now the strings don't match each other. And any time you add or remove the last apostrophe, you've got to switch the string quoting style. If you do that on the last string, you also need to toggle the escaping on the other quote characters in the string.
Is this really an improvement over just letting people do what they want?
Not an improvement.
I'd say: simple case – use single quotes.
Non-simple case – do what you want.
On Fri, Mar 15, 2019 at 1:38 PM Bob Nystrom notifications@github.com
wrote:
We could say that, yes, but then that leads to:
takesManyStrings(
"this first string's got an apostrophe",
'this does not',
"this's got one",
'not this',
"this one's got "both" kinds");Now the strings don't match each other. And any time you add or remove the
last apostrophe, you've got to switch the string quoting style. If you do
that on the last string, you also need to toggle the escaping on the
other quote characters in the string.Is this really an improvement over just letting people do what they want?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/site-www/issues/859#issuecomment-473433179,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCitI_uvnCBLUkPRBjwd1s_fg6SSPxks5vXASygaJpZM4T5GaP
.
Flutter prefers single quotes - https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#prefer-single-quotes-for-strings
Bob's comment from https://github.com/dart-lang/site-www/issues/859#issuecomment-473433179 still applies to that recommendation.
The rule doesn't say why I should use double quotes for nested strings. It's not necessary, you can use single quotes there too: 'This is a ${'nested string'}.'. Maybe it's because it's more readable to alternate the quotes, so you can better see the matching quote pairs. Then you'd want 'this is a ${"nested ${'string'}"}'?
And how about "That's a nested ${"String"}?". Should I use double quotes for that nested string, even though the outer string uses double quotes too, or should I be alternating.
A rule as simple as the Flutter one doesn't work in all cases, and making the rule too complex removes the benefit of having a rule (user's will have to think, not just do). If the fallback is "do what you want", and all it takes to hit that case is to have an apostrophe in the text, we might as well just go with "do what you want" as the only rule. (Which is the current state, precisely because it has not been possible to find a universally better set of rules).
I'm going to close this, since we haven't been able to come up with a good recommendation.
Most helpful comment
We could say that, yes, but then that leads to:
Now the strings don't match each other. And any time you add or remove the last apostrophe, you've got to switch the string quoting style. If you do that on the last string, you also need to toggle the escaping on the other quote characters in the string.
Is this really an improvement over just letting people do what they want?