So you randomly have single and double quotes throughout the codebase?
I don't care which one a project uses, but it seems wise to pick one. Uniform syntax is very helpful for code readability.
So you randomly have single and double quotes throughout the codebase?
I don't care which one a project uses, but it seems wise to pick one. Uniform syntax is very helpful for code readability.
I'm personally part of the second group simply because my linter forced it, but at the end of the day, it's such a low impact we don't really care.
So in that case, they're not used randomly according to the individual user's preference. There's a consistent rule. It's helpful for readability, too. And not sure about Rubocop in Ruby, but many linters in other languages can pick up on these different applications.
Rubocop would have you use singles for non interpolated and double for interpolated.
I get that there is a potential to catch some kind of error where you use double quotes on a string that will interpolated something which it wouldn't if it were in single quotes. In 12 years, I've never had that happen. I have, however, had it happen dozens of times where I changed something from interpolated to not and then I have to bounce over to change the quotes. This happens a lot and is annoying.
It's also an eyesore when you have an array of messages, each neatly aligned, but some are interpolated and some aren't, so the quotes change.
In Ruby, knowing that a string has interpolation is easy enough by seeing the #{} in the middle of it. It's hard to miss, really.
The single/double quote cop which is turned on by default is also counter to the existing style of every-string-is-double when it came along.
Fair enough. I'm not much into Ruby but if I were, that's what I would encourage. But I'd be ok with whatever house style, as long as it's consistent.
irb(main):001:0> puts '1\t2',"1\t2"
1\t2
1 2
> In Ruby, knowing that a string has interpolation is easy enough by seeing the #{} in the middle of it.{} are not required for instance variables.
I don't understand this argument. Are people really having trouble reading code because it has two different quote characters? This goes against my understanding of "readability"? What is it that is really meant by this?
Add in programming styles and a flexible language like Ruby can look like entirely different languages from one dev to the next (Ruby is a very traditional OOP capable language, or a weird but capable lisp, mix the two and readability suffers in new and exciting ways).
Personally I have a few reasons why singlequotes are inferior:
- String interpolation requires doublequotes
- Using an apostrophe requires you to escape it or use doublequotes
- They're harder to type on a Japanese-layout keyboard, which I use (it's shift+7, lol), which is otherwise really efficient for Ruby
But there are arguments against doublequotes too:
- I guess they're maybe harder to type on an ANSI keyboard?
- They parse ASCII escape codes, and singlequotes don't
But in my experience I need to use interpolation and apostrophes more than I care about not parsing \r so I use doublequotes everywhere in my own codebase.
So I would just rather not force either, as people seem to feel strongly both ways and the added frustration is probably worse for productivity than having it look a bit neater :)
Honestly I think the "community" chose wrongly in trying to standardise on singlequotes-unless-you-need-interpolation, when it should probably be doublequotes-unless-you-have-a-good-reason, but it's pretty hard to change that now.