People! Please Stop Using Single Quotes!

Posted November 7, 2006 at 2:10 PM

Tags: ColdFusion

On my journey to ColdFusion mastery I look through a LOT of code. One thing that irks me to no end is the hugely prevalent use of singles quotes:

 Launch code in new window » Download code as text file »

  • <cfset url = 'http://localhost/ilove/single/quotes/' />

I see this and it drives me bonkers! These people must not code inside of an IDE that has color coding (are you still coding in Notepad?). Most IDEs that I have used don't have special default color coding for single quotes. For double quotes, on the other hand, they do (usually a nice, easy to read blue). It makes reading the code soooo much easier.

So, are you trying to make your code hard to read (I can respect that, after all, your code, your rules)? I thought maybe it had to do with "old school" programming... but if memory serves me correctly, aren't single quotes used for characters and double quotes used for strings?

Someone please end the madness. Can someone give me a single good reason (other than personal preference) why single quotes should be used around strings and attributes?

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page




Reader Comments

Nov 7, 2006 at 2:41 PM // reply »
1 Comments

Dreamweaver color-codes single- or double-quotes. So does Eclipse if memory serves.

Can't use double-quotes in SQL queries. If I recall, single-quotes are also preferred in JavaScript. I've never programmed in C, but I think single-quotes are preferred there, too.

That said I prefer double-quotes. They're easier to see, I think.


Nov 7, 2006 at 2:49 PM // reply »
7,572 Comments

Al,

I haven't used Dreamweaver (I still use Homesite) and I don't recall for Eclipse. I can tell you, however, from years of experience that Javascript has no sense of good or bad for single quotes. In fact, if I had to say anything, I would say it's better to use double quotes in Javascript so you don't have to escape single quotes within strings:

var strTest = 'Single quotes! They\'s bunk!';

vs.

var strTest = "Single quotes! They's bunk!";

Notice that I don't have to use any unnatural looking escaped characters for often used single quotes. More often than not, I find, you need to use nested single quotes than nested double quotes.


Nov 7, 2006 at 3:19 PM // reply »
6 Comments

Ben, I also HATE the use of single quotes in attributes. I can only think of one time that I use them. If I'm trying to produce a line of HTML output, say:

<a href="http://www.cnn.com">CNN</a>

If I were creating that whole string inside a cfset, I might wrap it with single quotes, so that I didn't have to escape the double-quotes.

Other than that, I can't think of a reason you shouldn't be whipped with a wet noodle for using them.... ;-)


Nov 7, 2006 at 3:21 PM // reply »
7,572 Comments

Demian,

Ha ha, wet noodle :) Nicely put.


Nov 7, 2006 at 3:59 PM // reply »
92 Comments

Got to go with Demian on that one! Its the only time I've used single quotes over double quotes. The thing is I have known some developers who loved using single quotes all the time. In fact one of them I work with. I only remember asking once to a developer last year why he used single quotes all the time. Only response I got was that he found it much easier to read his code or others.


Nov 7, 2006 at 4:04 PM // reply »
7,572 Comments

Yeah, I guess it all comes down to personal preference. I personally find single quotes harder to read. They seem too "inline" to me. Double quotes give my eye more of a visual break as if to say "Yo dude, I am about to start or end an important value."


Nov 7, 2006 at 6:40 PM // reply »
64 Comments

Eclipse certainly colour-codes single-quoted strings. What other IDE for CF is there? ;-)

I find it's horses for courses. If I need to embed a double-quote in a string, I'll wrap it in single quotes (preferring this to escaping the double-quote). Other than that, I err towards double-quotes for strings.

In the bigger scheme of things when it comes to maintaining other people's code... it's not really one of the major issues, I think.

--
Adam


Nov 7, 2006 at 6:46 PM // reply »
1 Comments

wft is wrong with you? wtf cares? post something useful. You're on goog - dont abuse the privilege with bs rants like this.


Nov 7, 2006 at 6:57 PM // reply »
7,572 Comments

Mike,

I have been told over my life that there is Plenty wrong with me.

But seriously dude, don't get all steamed. I try to keep my rants to a minimum and I try to keep the majority of my posts highly relevant and chock-full-o-code. However, I understand that my stylings are not for everyone. It's not personal. If you feel that my contribution to "Goog" is not useful, by all means request that I be removed. I will not take offense.


Nov 7, 2006 at 9:12 PM // reply »
2 Comments

Hey Ben, good work on your blog always good stuff here, forget that other wombats comments. I normally use double quotes just as a personal preference but came across the first occurrence today where I had to reverse this it was using

<cfheader name="Content-Disposition" value='attachment;filename="download a document Description.pdf"'>

I understood that I had to escape the spaces in the filename, however this would not work if I had double quotes around the value, this was using firefox to download. First time I came across this though.


Nov 8, 2006 at 1:06 AM // reply »
10 Comments

You obviously havn't done much coding.

After a while you will find many places where you need to resort to single quotes, and it eventually makes sense to do this evey where.

A simple example, ohh lets say putting any function in a HTML element.

<a href="#getUrl('home')#">#getTitle('home')#</a>


Nov 8, 2006 at 4:48 AM // reply »
15 Comments

Ooops! I just posted my comment to "Ask Ben". Sorry.
Fortunately FF kept the content, so I can re-post to the correct place:

For attributes (both HTML or CFML), I always use double quotes, even if it means escaping inner doubles.
For structures I always use single quotes.
For functions, I'll use single quotes if its a 'simple' variable - ie: empty or a one-word string.
If its a long string, or a cfset-ed string, I go for double (unless it would involve escaping, in which case I'll resort to single).

See, perfectly logical! :>


Nov 8, 2006 at 5:08 AM // reply »
2 Comments

Dale,

I understand what you have done the problem with my example is it only works if you escape everything with single quotes as soon as you try double quotes on the outside and single on the inside it no longer works.


Nov 8, 2006 at 5:20 AM // reply »
10 Comments

When you get into a lot of OO cfc stuff, you need single quotes more and more.

For example in our app every string goes through a translation macro. For language translations. So every string needs to have a function around it.

<h1>#tr('Heading')#</h1>
<p>#tr('The quick brown fox')#</p>
<a href="home.cfm">#tr('click here')#</a>
<img src="logo.gif" alt="#tr('Company Logo')#" />


Nov 8, 2006 at 7:32 AM // reply »
7,572 Comments

@Peter B.

I am glad that you have a methodology. That's cool man.

@Dale,

I assure that I have done many years of programming (though not as many as I would like). As for your examples, mixing CFML inside of HTML is moot point as they are both parsed at different times and in different places. Having double-quotes in a CFML call inside an HTML attribute has no side effects at all (except that the IDE is probably going to have some trouble with color coding):

<a href="#UrlEncodedFormat( "sweeet.htm" )#"></a>

In this case the double quotes in the method argument do not interact with the double quotes of the HREF attribute in any way.

I am not sure what OOP and string translation has to do with double quotes?


Nov 8, 2006 at 9:04 AM // reply »
16 Comments

Single quotes are the lazy man's double quotes.
You don't have to press the shift key to type a single.
And yes, there is something wrong with that.


Nov 8, 2006 at 9:08 AM // reply »
7,572 Comments

Ha ha, nice :)


Nov 8, 2006 at 9:18 AM // reply »
92 Comments

I just thought I'd point out how funny it is that a commenter on here named Mike complained about what he considered a useless blog post that happens to plague Goog. Personally, I've never used Goog so I guess I'm very biased when I say who cares but the point is after reading several of your blog posts for the past 2-3 months this definitely has to be the one with the most comments. Funny how sometimes the most simple or awkward, or even useless blog post can be the one most commented on. Goes to show what some people know!


Nov 8, 2006 at 9:22 AM // reply »
7,572 Comments

Thanks Javier, that means a lot to me :)


Nov 8, 2006 at 10:30 AM // reply »
25 Comments

For Mike, the goog complainer, this is way less off topic than most posts. One of the great things about goog and other aggregators is you get the title and a sentence or two. Skim and either drill down or skip. Don't go everywhere and complain.

To get back on topic, Ben, personally I like single quotes when passing parameters to a function or referring to a structure key, but I agree that double quotes should be used for attributes, setting variables, etc.

I don't have a good reason for this, just my preference. However, if faced with working on a project where others are doing it differently, I can adjust to be consistent.


Nov 8, 2006 at 1:33 PM // reply »
92 Comments

Hey no problem man. Us CF developers got to look out for one another. You share your hard work and code with us and I do my best to share my support. The Family provides quality protection. Fogehaboutit! :)


Apr 18, 2007 at 3:03 AM // reply »
1 Comments

Single quotes are better because they use less screen ink.


Dec 29, 2007 at 10:43 AM // reply »
1 Comments

I use single quotes exclusively, because it is far easier to integrate into the PHP code my programmer buddy writes. He prefers double quotes, so it is a matter of making his life easier.

DW and Aptana/Eclipse color-code single quotes, but DW doesn't let one autocomplete with single quotes which is why I don't use DW.


Jan 14, 2008 at 10:28 PM // reply »
1 Comments

most single quotes are from dynamic pages that has php spit out the content....its easier to use single quotes...

about 50/50 ill escape the double quotes versus the single '

\" is just more work...


Jan 18, 2008 at 10:29 AM // reply »
1 Comments

Putting text in double quotes adds an extra step for the processing engine: the string is read in, and then re-interpreted to parse out any special characters, variables within, etc. Whereas a single-quoted string is just read in and that's it.

So, if you're taking performance optimization into account, you should use single quotes for setting text strings, if you want to save some processing overhead. How much overhead can you save? Probably negligible on a small-scale site, but as a site gets larger / more traffic, it can be significant.

In any case, it's always better to escape out of strings when adding variables, i.e.

<cfset out = 'this variable is: ' & variables.rockin & ', right?'>

it's also best to avoid using the ## syntax as much as possible


Jan 20, 2008 at 10:17 AM // reply »
7,572 Comments

@Chip,

I am not sure whether avoiding the ## in strings actually will save you any overhead. ColdFusion still has parse each string looking for uses of #variable#, so I don't think you are going to save much. Plus, I would hope that the ColdFusion compiler would be smart enough to, behind the scenes, replace ## string variables with something that is optimized.


Apr 2, 2008 at 11:11 PM // reply »
1 Comments

I like double quotes better. It feels more.... whatever it feels, it feels more of it using double quotes. No reason.

That said, single and double quotes are processed differently in several languages such as php; single quotes enclose a string; you can put a variable in it (for instance: 'here is a $variable to use') and it will be passed as a string. If you enclose that same string in double quotes, php will try to process the $variable. A quick and easy shortcut if you don't want to write "here is a " . $variable . " to use".


May 22, 2008 at 11:56 AM // reply »
1 Comments

You know what grinds my gears? Working on someone's code when they've used the literal single quote characters in a replace function. It screws up the syntax highlighting in Eclipse for the rest of the file.
#Replace(myString,"'","''","all")#

I end up changing it to
#Replace(myString,chr(39),"#chr(39)##chr(39)#","all")#

Just my personal preference... Keeps my code clean and readable.


Jul 3, 2008 at 1:29 PM // reply »
1 Comments

If the html is produced by a server-side script, single quotes can be easier to code, the compiler will ignore special characters in single quotes, but not double quotes.

For example (php):

$url = "http://bennadel.com";

echo "<a href='$url'>Click here</a>"; // Creates a link to bennadel.com
echo '<a href="$url">Click here</a>"; // Creates a link to $url

Not to mention some people just get into the habit of using single quotes from writing lots of SQL.

You can probably change a setting in whatever you're using to write html, I use eclipse and it changes the color of both single and double quotes.


Ben
Apr 21, 2009 at 3:05 PM // reply »
1 Comments

An important difference:
In a cfquery, single-quoted strings get escaped, even without cfqueryparam. Double-quoted strings get passed right through. So if you're preventing SQL injection the lazy way, single-quotes will help but double quotes won't.


Dec 18, 2009 at 4:55 AM // reply »
1 Comments

how can fine the special charactors in JS. Any example plz


Feb 4, 2010 at 8:32 AM // reply »
1 Comments

I prefer single-quotes in PHP and JavaScript.
Double-quotes are required in XHTML attributes which makes it not a matter of preference but syntax.

In PHP double-quotes are subject to string interpolation which expands \t, \n, \r, etc. and variables. Using single-quotes avoids PHP to do this, usually, unecessary interpolation.


Feb 4, 2010 at 9:17 PM // reply »
7,572 Comments

@Thomas,

Coming from ColdFusion, I had never seen that before. But, now that I am playing around with Groovy, I see similar functionality - double quotes are evaluated, single quotes are not. This definitely changes the game.... IF you work with a language that differentiates.


Feb 5, 2010 at 7:33 AM // reply »
1 Comments

The argument for not having to escape doesn't really hold water, nested quotes happen in both directions.

consider:
I've always used "Hello, World!" as an example.

Just to note, the same is true of Perl as in PHP, double quoted strings are interpolated. So single quotes are the norm for string literals to avoid the expression engine invocation as well as parsing for nested variables.

Also, what editor are you using that you can't fix this code-coloring with a simple style tweak? Maybe the fix is to get a more robust editor? :P


Feb 5, 2010 at 4:54 PM // reply »
7,572 Comments

@Thomasb,

My editor is definitely a bit old. It has color coding, but I don't think it has quote-type color coding.


Feb 10, 2010 at 6:51 PM // reply »
1 Comments

"Can someone give me a single good reason (other than personal preference) why single quotes should be used around strings and attributes?"

Hello,

I found a spot where single quotes are necessary around strings: when passing json strings that contain quoted elements during an AJAX request using ColdFusion.

I stumbled on this when I tried to pass the following json string back to javascript:

<cfset json = '{"results":"Here is a "quoted string" i am sending."}'>

Only, in ColdFusion, the quotes surrounding "quoted string" need to be escaped. So I got:

<cfset json = '{"results":"Here is a ""quoted string"" i am sending."}'>

However, the string, when sent to output:

<cfscript>
WriteOutput(json);
</cfscript>

Looks like this is firebug's Net > Response tab:

{"results":"Here is a ""quoted string"" i am sending."}

Which JavaScript won't eval properly.

So, the solution is to concatenate a bunch of strings together and begin the strings with a single if you need an inner double, or begin with a double if you need an inner single:

<cfset json = '{"results":' & "'Here is a" & '"quoted string"' & " i am sending.'" & "}" />

Which is sent to JavaScript, and eval'd correctly, as:

{"results":'Here is a "quoted string" i am sending.'}

Note I am enclosing the name part of the name/value pair in this object in quotes for compatibility with Internet Explorer.


Feb 10, 2010 at 10:12 PM // reply »
7,572 Comments

@John,

While you typically do have to escape your qoutes in ColdFusion by doubling up on them, since your original JSON value was wrapped in single-quotes, you actually don't need to escape the double ones; this is why it was coming through unespaced in the AJAX. Your first approach:

<cfset json = '{"results":"Here is a "quoted string" i am sending."}'>

... was fine at the ColdFusion level since the single-quotes are the "containing" delimiter. However, this will break in the Javascript layer because at that point, the Javascript string value:

"Here is a "quoted string" i am sending."

... has unescaped quotes. In Javascript, however, the method of escaping is different. Special characters are escaped with back-slashes:

"Here is a \"quoted string\" i am sending."

Putting these two together, your can use the string:

<cfset json = '{"results":"Here is a \"quoted string\" i am sending."}'>

This stuff is definitely crazy and you have to think at multiple levels. All I have to say it, thank goodness that CF8 gave use serializeJSON() and deserialiseJSON() functions!


Feb 19, 2010 at 5:21 PM // reply »
78 Comments

Do you still feel the same way about single quoting? I prefer it because you don't have to hit the shift button to get them like you do with double quotes.

Plus it's easier to tell what type of quote it is when working with smaller point size editors.


Feb 19, 2010 at 5:26 PM // reply »
78 Comments

@Thomas...

The xHTML spec simply says that all attributes must be quoted, it does not say whether single, or double, should be used:
http://www.w3.org/TR/xhtml1/#h-4.4

So it just comes down to personal preference. What most people use for HTML attributes are double quotes.


Feb 20, 2010 at 10:41 AM // reply »
10 Comments

Now when Andy and John raised this topic from dead , I must say a word about this :)))

After reading all comments here and (re)evaluating my opinion about it, I must say: It is all about a personal preference, that is "De gustibus non est disputandum".

For many year I used double quotes for strings and it came from C where double quotes surrounds strings and single chars. But, when my last client imposed opposite rule by their coding guidelines I found quite easy to adopt to it, and now, even I am not working for that client, I continue using it.

(kid woke up, must leave :))


Feb 22, 2010 at 8:23 PM // reply »
7,572 Comments

@Andy,

I am still a big double-quote fan. Of course, I am also a big white space fan. I think to me, part of the double-quote issue is that it simply makes a LARGE delimiter in my code that I can visually parse faster.

Plus, since I am still in HomeSite, single quote are not color coded in the same way.

At the end of the day, people just need to be consistent. There's nothing that frustrates me more than irrational coding standards. People throwing double quotes here and single quotes there (in a language where there is no processing difference)... totally unacceptable :)

@Marko,

Yeah, I think it's just personal preference.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »