uno o dos
Posted by jcarroll
Apr 01
Now in Ruby, strings can be created using single or double quotes. The only difference is that you can interpolate variables in double quoted strings. I constantly see the following confusing code:
1 2 3 |
user.name = "name" link_to "home", root_path |
Here we’re using double quoted strings, but there’s no interpolation. As soon as I see double quotes, I think there’s going to be some interpolation. Using double quotes without interpolation is flat out wrong. The above code is more correct, and much less confusing using single quotes.
1 2 3 |
user.name = 'name' link_to 'home', root_path |
This may not seem like much, but it makes a huge difference when followed consistently.
Comments on this post
Apr 01
Pete Forde said,
I think you make a good argument, but I will likely continue to use double quotes in my code.
Simply, I can see myself losing more time wondering why adding an interpolation broke my function, than I can see myself saving by knowing if an interpolation is present.
I say this because I’ve never encountered a situation where it specifically mattered if there was an interpolation used, or not. What scenarios are you basing this off of?
Apr 01
matt jankowski said,
I agree thoroughly.
Imaginary benchmarks have shown a huge performance gain from using single quotes when you don’t need interpolation.
Apr 01
Joe Grossberg said,
“Using double quotes without interpolation is flat out wrong.”
FWIW, you’re the first person I’ve heard mention the double-quotes as signalling a string interpolation that they’ll reflexively look for. But if you’re using a decent code editor, both types of quoted strings should be the same color/style anyway.
And, while you don’t bring up the common complaint of inefficiency, changing from double-quotes to single-quotes is the most overrated code optimization, IMHO.
If someone “fixed” that code, I would tell them that they were making sub-optimal use of their time.
Apr 01
Alice said,
<<EOF Every string should be a HEREDOC and that is a fact. EOF
Apr 01
Dan Croak said,
Single quotes are the Lou Gehrig to double quotes’ Babe Ruth. Sure, you can sock some dingers with double-quoted interpolation. They’re every cancer patient kid’s favorite player. But you don’t swing for the fences with every string. That’s why you gotta go with single quotes for clean-cut reliability and svelt speed.
Apr 01
Andrew Vit said,
Is it a good use of the programmer’s brain to wonder ahead of time which string should be single or double quoted, and then to be fixing things afterwards when a #{variable} needs to be added into a single-quoted string?
Admittedly, I use single quotes too because I learned there was a performance penalty for interpolated strings back in PHP3 or somesuch. But in reality, I’m finding that it’s more trouble than it’s worth to make the distinction between them. These days I’m leaning towards “just use doubles” and fuggedaboudit.
Apr 01
Nicolás Sanguinetti said,
The funny thing is, when I see a single-quoted string I usually think the opposite of what you think when you see double quotes :)
I just say “dos”: I use double quotes. Maybe because I’m from a Spanish-speaking country, and Spanish doesn’t use single quotes, but whichever the reason is, I always use double quotes unless I need single quotes to avoid interpolation.
I guess it’s in the eye of the beholder and all that.
Apr 01
nicolash said,
the culprit is textmate… it is just a hassle to change them by hand…
Apr 01
bryanl said,
I’m with Nicolás, I use single quotes to avoid interpolation rather than the other way around.
Apr 01
James said,
Is this an April fool’s joke? Seriously?
Apr 01
jareCare said,
oh no james,
this is all very real and something we’ve discussed at length and yet still stay divided on here at tbot
Apr 01
matt jankowski said,
It’s very very real.
If you’ve never tried single quotes, please do. You won’t go back.
Apr 01
josh said,
I’m the other way around here. I prefer double quotes, and only use single quotes if I have to embed a double quote in the string. Or, I’ll use the %Q operator to quote double quotes. The number of times I’ve had to convert a single quoted string to use double quotes because I later decide to add some interpolation is far more than the number of times I realized I needed single quotes to contain a double quote. The only time I generically prefer single quotes is for the replacement string in a #sub or #gsub method.
Apr 01
Floyd said,
The only time in which single quote is remotely useful is for nested quotes, and in this case it is really just shorthand for a perfectly good escape. I am at a loss at how you can possibly think to argue via some perverted concept of elegance that “signaling” interpolation trumps the simplicity of just only using one type of quote. Why can’t your editor highlight interpolations if you are caught off guard so easily? I’m having visions of you coding along on some speedy refactor, coming along a double quote w/o interpolation and falling back out of your chair and walking around the rest of the day on crutches. So tell me, is it that, or have you just come up with this totally imaginary “objective” measure to justify your personal style?
Apr 02
Neil Wilson said,
Good April Fool
Apr 02
Rob Sanheim said,
Nice one…had me for a few seconds.
Apr 07
Jon Yurek said,
Incidentally, we benchmarked single versus double quotes and came to the startling realization that the number of man-hours spent writing, reading, and commenting on this post exceeds 5,000,000 times the amount of time actually saved by using single versus double quotes.
Sorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Come “ride the toad” on Hoptoad, the app error app.
Thunder Thimble: Brand monitoring for social media.
Widgetfinger: Simple content management for simple websites.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our beginning or advanced training.
Archives