What is your style? Single quote, double quotes or depends?

139 views
Skip to first unread message

Brandon

unread,
Mar 13, 2014, 12:52:37 AM3/13/14
to rubyonra...@googlegroups.com
I'm curious what is the best practice here.. Do you use single quote for both .html.erb and .rb files?

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. 

What has been your experience?

tamouse pontiki

unread,
Mar 13, 2014, 12:56:10 AM3/13/14
to rubyonra...@googlegroups.com
It depends upon whether I need to do any interpolation in the string or not.

Dheeraj Kumar

unread,
Mar 13, 2014, 12:57:43 AM3/13/14
to rubyonra...@googlegroups.com
I use single quotes for the same reasons as you :) I use double quotes only when I'm doing interpolation.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/da8fe37b-5bc2-418b-b9bc-975747cd81f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Emil S

unread,
Mar 13, 2014, 1:12:54 AM3/13/14
to rubyonra...@googlegroups.com
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.



--

Dave Aronson

unread,
Mar 13, 2014, 8:01:46 AM3/13/14
to rubyonrails-talk
On Thu, Mar 13, 2014 at 12:52 AM, Brandon <wong...@gmail.com> wrote:

> 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. 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.

-Dave

--
Dave Aronson, FREELANCE SOFTWARE DEVELOPER LOOKING FOR REMOTE CONTRACTS
(or temp jobs, or in/near Fairfax VA; see www.Codosaur.us for details);
see also www.PullRequestRoulette.com, Blog.Codosaur.us, www.Dare2XL.com

tamouse pontiki

unread,
Mar 15, 2014, 1:53:24 AM3/15/14
to rubyonra...@googlegroups.com
On Thu, Mar 13, 2014 at 7:01 AM, Dave Aronson
<googlegr...@davearonson.com> wrote:
> On Thu, Mar 13, 2014 at 12:52 AM, Brandon <wong...@gmail.com> wrote:
>
>> 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.

Gurpreet Luthra

unread,
Mar 15, 2014, 4:09:59 AM3/15/14
to rubyonra...@googlegroups.com
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



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Walter Lee Davis

unread,
Mar 15, 2014, 10:02:31 AM3/15/14
to rubyonra...@googlegroups.com

On Mar 15, 2014, at 4:09 AM, Gurpreet Luthra wrote:

> 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.

Walter
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHKR60yKeVwfvRTWdLrg64qA5EUTocAG%3Dsno496Gz5AG9CA2pQ%40mail.gmail.com.

tamouse pontiki

unread,
Mar 15, 2014, 10:50:18 PM3/15/14
to rubyonra...@googlegroups.com
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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/01E4C7D3-90F2-44CC-AD1E-6F2E38A3938D%40wdstudio.com.

Brandon

unread,
Mar 16, 2014, 12:41:24 AM3/16/14
to rubyonra...@googlegroups.com
Great teaching guys. I started turning everything to single quote until I read the potential future problems here.

But would you be using double quotes every where (including Javascript) to keep in all standard?

Dave Aronson

unread,
Mar 16, 2014, 6:07:54 PM3/16/14
to rubyonrails-talk
On Sun, Mar 16, 2014 at 12:41 AM, Brandon <wong...@gmail.com> wrote:

> But would you be using double quotes every where (including Javascript) to
> keep in all standard?

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....)

Jordon Bedwell

unread,
Mar 16, 2014, 8:00:52 PM3/16/14
to rubyonra...@googlegroups.com
On Sun, Mar 16, 2014 at 5:07 PM, Dave Aronson
<googlegr...@davearonson.com> wrote:
> On Sun, Mar 16, 2014 at 12:41 AM, Brandon <wong...@gmail.com> wrote:
>
>> But would you be using double quotes every where (including Javascript) to
>> keep in all standard?
>
> 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....)

There isn't any difference other than them existing together to
support stuff like "hello 'world'" and junk like that.

Andy Jeffries

unread,
Mar 17, 2014, 10:13:14 AM3/17/14
to rubyonra...@googlegroups.com
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.

Cheers,


Andy

Colin Law

unread,
Mar 17, 2014, 11:32:44 AM3/17/14
to rubyonra...@googlegroups.com
On 17 March 2014 14:13, Andy Jeffries <an...@andyjeffries.co.uk> wrote:
> According to a quick benchmark on my machine (MacBook Air 2013, Ruby
> 2.1.0p0), using double quotes is about 2% faster:

Or, possibly, slower.

>
> 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)

Did you try swapping the two lines round and checking that the result
is consistent?

Colin

Andy Jeffries

unread,
Mar 25, 2014, 11:10:40 AM3/25/14
to rubyonra...@googlegroups.com

On 17 March 2014 15:32, Colin Law <cla...@gmail.com> wrote:
Did you try swapping the two lines round and checking that the result
is consistent?

Yes, it's consistent for me.  Was it not for you or are you just asking?

Cheers,


Andy

Colin Law

unread,
Mar 25, 2014, 11:24:45 AM3/25/14
to rubyonra...@googlegroups.com
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.

Cheers

Colin

Andy Jeffries

unread,
Mar 26, 2014, 11:51:29 AM3/26/14
to rubyonra...@googlegroups.com

On 25 March 2014 15:24, Colin Law <cla...@gmail.com> wrote:
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.

Doh, fair point, thanks for catching that :-)

Cheers,


Andy

Andy Jeffries Ruby on Rails, RubyMotion, jQuery Developer & Taekwondo 6th Dan Instructor
andyjeffries.co.uk +44 7939 164853 @andyjeffries fb.com/andyjeffries
Reply all
Reply to author
Forward
0 new messages