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 get to see a lot of different codebases. The most common quote style I see is using single quotes by default and double quotes when interpolating. When I ask why I always get the same suspicious answers:
- The style guide says to use single quotes.
- Single-quoted strings are more performant.
- You know at a glance whether a string is being interpolated or not.
I’m going to try and settle this once and for all.
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 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 guide, but if anything double quotes are preferred. At best, you could argue there is no consensus.
I understand where this myth comes from—it sounds plausible. I was going to write a bunch of 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.
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? Most of my strings fit on one line. I can tell at a glance whether or not interpolation is happening by just looking at the string.
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 parse single-quoted strings than double-quoted strings. I have no empirical proof for this—it’s just anecdotal observation—but when I point it out most seem to notice the difference.
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.
Look at that, it turns out there’s a performance difference after all.