There’s no best practice here. The ruby style guide which Rubocop depends on, which is kind of popular, allows both conventions. The key here is to pick one style and stick with it. My team here uses Rubocop with one of the styles and we use overcommit to make sure every line of code that’s committed sticks with a set of conventions that we all agree on.
I've started doing the opposite. The tiny time savings in the
interpreter from not checking for interpolation, pale in comparison to
the human-time wasted by "why is it failing to do the interpolation I
added, oh, I didn't convert the quotes" and "why is it blowing up on
this perfectly innocent string, oh, I used a contraction inside
single-quotes so it thinks the string ends there" and whatever other
problems it causes. If you've got some special case of a frequently
run tight loop where shaving off a few microseconds by using single
quotes to forestall interpolation-checks might actually help, go for
it... but otherwise it's premature optimization. Go ahead and get in
the habit of pressing the @#$%^&* shift key, most programmers need all
the exercise they can get anyway.
Meanwhile, though, I'm curious. Can you articulate why you feel
single-quotes have better readability? I suspect it's a matter of
what other languages you're used to, both human and computer. In
English, most books enclose most quotes in double-quotes, versus the
<< and >> used in many mainland-European languages, and whatever else
may be used elsewhere. (I am not aware of any that use mainly
single-quotes. Anybody?) Prior to Ruby, most of my career was in C,
where double-quotes are required for strings -- it threw me a bit when
I first got into JavaScript where single-quotes seem to be the
predominant style.
Also, what do you mean "it takes up less spaces each line"? Either
one is a single character. Are you coding in a proportional-width
font? If so, I think you'll find it easier to visualize things about
your code if you code in monospace.
I try to use single quote first as much as I can because I find the
readability is better and it takes up less spaces each line.
I've started doing the opposite. The tiny time savings in the
interpreter from not checking for interpolation, pale in comparison to
the human-time wasted by "why is it failing to do the interpolation I
added, oh, I didn't convert the quotes" and "why is it blowing up on
this perfectly innocent string, oh, I used a contraction inside
single-quotes so it thinks the string ends there" and whatever other
problems it causes.
I can get behind this. My fingers tend to land on single quotes for
certain things I have *never* used interpolation on, such as require
names. Not that one couldn't, I just haven't (yet). I struggled with
watching a senior dev experience this very thing and not feeling I
should say anything to them. I finally decided I should.
Meanwhile, though, I'm curious. Can you articulate why you feel
single-quotes have better readability?
I'm actually now thinking that the double quote marks out a string
more easily visually, and so probably makes it more valuable for that.
But not much.
RubyMine gives a warning on any string that uses double quotes without interpolation… So I learnt to use single quotes when no interpolation, and double quotes only on interpolation.
RubyMine gives a warning on any string that uses double quotes without interpolation... So I learnt to use single quotes when no interpolation, and double quotes only on interpolation.
I would prefer using double quotes everywhere..
Regards
Gurpreet
I'm ashamed to admit this, but I come from PHP, where there is (or used to be) a subtle performance hit when using double-quotes. It was explained to me as being similar to the difference between a .html and .php file to the Apache server -- the former requires no introspection, just find it and serve it, while the latter requires careful inspection and delegation. I tend to use single-quotes unless I want to use interpolation.
It's a similar *concept*, but in no way is it similar in performance
degradation. It is in no way one with a serious enough difference to
matter when there are probably more useful bottlenecks to go after,
but only *after* you profile and collect data. Premature performance
optimisation should be avoided unless there is data already that shows
something will make a difference. Otherwise, the beautiful code mantra
applies: 1. Correct. 2. Pretty. 3. Fast.
Good question. For JS that's part of a Rails project, I think I
would, now that you raise the point. For other JS, well... single
seems to be the standard, and I don't think JS makes any difference at
all between them. Anybody know of a difference? (I mean, they gotta
match, but other than that....)
According to a quick benchmark on my machine (MacBook Air 2013, Ruby 2.1.0p0), using double quotes is about 2% faster:
require ‘benchmark’
n = 10_000_000
Benchmark.bm do |x|
x.report { n.times do ; a = “1”; end }
x.report { n.times do ; a = ‘1’; end }
end
user system total real
1.020000 0.000000 1.020000 ( 1.015903)
0.990000 0.000000 0.990000 ( 0.992175)
However, as you can see it takes about 1 second to do 10 million iterations, so in reality, using a " instead of a ’ adds on about 2 nanoseconds on a 99 nanosecond operation. There are much bigger inefficiencies in all of our code than worrying about saving the odd 2ns here and there…
I personally tend to use double quotes everywhere, for the same reason that I never do if statements on multiple lines (in Javascript for example) without braces. I’d much rather have the almost unmeasurable wastage than risk a bug.
My comment "Or, possibly, slower" was to point out that you had said
double quotes were 2% faster when I believe you meant slower.
My consistency question was prompted by that fact that when there are
such small differences one has to be careful that one is seeing a real
effect, which apparently you were, so that is all right.