Always Use Double-quoted Strings in Ruby
Single-quoted strings or double-quoted strings; the eternal conflict rages on.
Between years of freelancing and open-source work I’ve gotten to see a lot of different codebases and the most common quote style I see is using single quotes by default and double quotes only when interpolating. When I ask why I always get the same answers:
- The style guides say to use single quotes
- Single-quoted strings are more performant
- You know at a glance whether a string is being interpolated or not
Let’s go through these one at a time and then the reasons why I think using double-quotes is better.
Single quotes
Style guide
There are a number of style guides, but the two most popular ones are Bozhidar Batsov’s and GitHub’s, which is based on Bozhidar’s.
Bozhidar’s enumerates the two options—using single quotes as a default and double quotes when interpolating, or just using double quotes all the time—but only recommends being consistent in whichever you choose. In a previous version he notes that the latter style is a bit more popular in the Ruby community, although I have no idea if that is true or not.
GitHub’s style guide says to just use double quotes all the time.
RuboCop is an awesome tool built by Bozhidar that analyzes code for style guide violations. The default configuration is to use single quotes. However, Hound, a service built on top of Rubocop by Thoughtbot defaults to double quotes.
I don’t know where the idea came from that single quotes are preferred in the style guides, but if anything double quotes are preferred. At worst, you could argue there is no consensus.
Performance
I understand where this myth comes from—it sounds plausible. I was going to try to do some benchmarks but Lawson Kurtz already did it. It’s possible there was a performance difference at some point in the past, but if there ever was, there hasn’t been for a long time.
Intent
I find this one especially curious. Exactly how long a string are we talking about? How far offscreen horizontally or vertically are these strings going that you need to be able to discern from a quote mark at the beginning or end whether or not interpolation is happening? If you really need to tell whether a string is being interpolated or not, I think most of the time you can do it by just looking at the string.
Double quotes
Readability
My favorite reason for using double-quoted strings is that I think they’re easier to read. Single quotes are not particularly distinctive—they’re too similar to too many other characters. When intentionally looking the difference is obvious, but when quickly scanning text it’s slightly harder to visually parse single-quoted strings than double-quoted strings. I can’t say this is objectively true—it’s just an anecdotal observation—but when I point it out most people seem to notice the difference.
Simplicity
If you don’t agree that double-quoted strings are more readable that’s fine, but that doesn’t mean it’s a draw and using either is equivalent. If you use both quote styles then every time you make a string you have to think about which quote style to use. You also have to change the quote style when you add or remove interpolation from an existing string. If you always use double quotes then you never have to waste any time thinking about it at all.