Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Suggest a more permissive use of string quoting. #5

Merged
merged 2 commits into from

7 participants

@fatbusinessman

Because there’s very little benefit to arguments in pull requests about whether to use single– or double-quoted strings. Let’s take this back to the problem we’re trying to avoid, namely hard-to-understand code.

@bradleywright

If this results in less code police comments on pull requests I'm all for it.

@alext
Owner

This gets my vote.

@benilovj
Collaborator

:+1:

@fatbusinessman

Nice try, @bradleywright, but I am wise to your trolling ways.

@edds
Owner

This seems to remove the reasoning in preferring to use double quoted strings in favor of a 'just do it because that is what we do'. Can we still have the reasoning but with the more permissive uses.

@alext
Owner

I'm not sure that I agree that the original reasoning was valid. Does it really hurt if there are a mixture of quoting styles?

It was also technically incorrect. There is a speed difference (albeit very small).

@bradleywright

If there's a speed difference surely we should default our advice to "use single quotes unless you require string interpolation, in which case use double quotes"? That's much clearer.

@fatbusinessman

@edds Good point; added.

@philandstuff

Suggest what whatever we end up with, we add the same to puppet.md to keep the styles consistent.

@fatbusinessman

@philandstuff Agreed, assuming it still makes sense for Puppet.

@KushalP
Owner

TL;DR The speed difference isn't that big until you start doing string interpolation (as we all expected).

Here's a quick benchmark you can run locally. I ran it as ruby strings.rb 2> /dev/null.

require "benchmark"

Benchmark.bm(14) do |x|
  x.report("single:") { (1..100000).each { warn 'Some string output' } }
  x.report("double:") { (1..100000).each { warn "Some string output" }}
  x.report("interpolation:") { (1..100000).each { |i| warn "Some string: #{i}" }}
end

The results I get:

$ ruby strings.rb 2> /dev/null 
                 user     system      total        real
single:         0.080000   0.010000   0.090000 (  0.099844)
double:         0.110000   0.000000   0.110000 (  0.106964)
interpolation:  0.110000   0.000000   0.110000 (  0.114989)

$ ruby strings.rb 2> /dev/null 
                 user     system      total        real
single:         0.070000   0.010000   0.080000 (  0.089104)
double:         0.080000   0.020000   0.100000 (  0.100067)
interpolation:  0.100000   0.000000   0.100000 (  0.117143)

$ ruby strings.rb 2> /dev/null 
                 user     system      total        real
single:         0.080000   0.000000   0.080000 (  0.091799)
double:         0.040000   0.040000   0.080000 (  0.095930)
interpolation:  0.110000   0.000000   0.110000 (  0.103764)

$ ruby strings.rb 2> /dev/null 
                 user     system      total        real
single:         0.040000   0.040000   0.080000 (  0.095917)
double:         0.100000   0.000000   0.100000 (  0.105139)
interpolation:  0.090000   0.020000   0.110000 (  0.121172)
@KushalP
Owner

Also, :+1: to permissive quotes.

@fatbusinessman fatbusinessman merged commit 5a7a5e3 into from
@fatbusinessman fatbusinessman deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2013
  1. Suggest a more permissive use of string quoting.

    David Thompson authored
  2. Clarify why double-quoted strings are sensible.

    David Thompson authored
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 12 deletions.
  1. +4 −12 ruby.md
View
16 ruby.md
@@ -603,18 +603,10 @@
email_with_name = "#{user.name} <#{user.email}>"
```
-- Use double-quoted strings. Most files will need some double-quoted strings,
- so it’s more consistent. Interpolation is in any case easy to see thanks
- to syntax highlighting, and there is no speed advantage to using single
- quotes.
-
- ```ruby
- # bad
- name = 'Bozhidar'
-
- # good
- name = "Bozhidar"
- ```
+- Try not to mix up single-quoted and double-quoted strings within a file:
+ it can make the code harder to read. *Definitely* don't mix up single-quoted
+ and double-quoted strings within a method. If in doubt, use double-quoted
+ strings, because you’ll probably need to use interpolation somewhere.
- Avoid using `String#+` when you need to construct large data chunks.
Instead, use `String#<<`. Concatenation mutates the string instance
Something went wrong with that request. Please try again.