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

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?

On Wed, Mar 12, 2014 at 11:52 PM, Brandon [email protected] wrote:

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?

It depends upon whether I need to do any interpolation in the string or
not.

I use single quotes for the same reasons as you :slight_smile: I use double quotes
only
when I’m doing interpolation.

There’s no best practice here. The ruby style
guidehttps://github.com/bbatsov/ruby-style-guide#stringswhich
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
styleshttps://github.com/bbatsov/rubocop/blob/d4639d03ac877db020044bd2f77ba5b933925f28/lib/rubocop/cop/style/string_literals.rb#L14-L20and
we use
overcommit https://github.com/causes/overcommit to make sure every
line
of code that’s committed sticks with a set of conventions that we all
agree
on.

On Thu, Mar 13, 2014 at 7:01 AM, Dave A.
[email protected] wrote:

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.

On Thu, Mar 13, 2014 at 12:52 AM, Brandon [email protected] 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. :wink:

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

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

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.

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?

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

On Sun, Mar 16, 2014 at 12:41 AM, Brandon [email protected] 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…)

-Dave


Dave A., 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

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

On 17 March 2014 14:13, Andy J. [email protected] 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.

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

On Sun, Mar 16, 2014 at 5:07 PM, Dave A.
[email protected] wrote:

On Sun, Mar 16, 2014 at 12:41 AM, Brandon [email protected] 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.

On 25 March 2014 15:10, Andy J. [email protected] wrote:

On 17 March 2014 15:32, Colin L. [email protected] 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?

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

On 25 March 2014 15:24, Colin L. [email protected] 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 :slight_smile:

Cheers,

Andy

Andy J. Ruby on Rails, RubyMotion, jQuery Developer & Taekwondo
6th
Dan Instructor
andyjeffries.co.uk +44 7939 164853
@andyjeffrieshttp://twitter.com/andyjeffries
Andy Jeffries http://facebook.com/andyjeffries

On 17 March 2014 15:32, Colin L. [email protected] 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