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 »
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
Comments (29) | Post Comment | Ask Ben | Permalink | Print Page
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.
Posted by Al Everett on Nov 7, 2006 at 2:41 PM
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.
Posted by Ben Nadel on Nov 7, 2006 at 2:49 PM
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.... ;-)
Posted by Demian on Nov 7, 2006 at 3:19 PM
Demian,
Ha ha, wet noodle :) Nicely put.
Posted by Ben Nadel on Nov 7, 2006 at 3:21 PM
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.
Posted by Javier Julio on Nov 7, 2006 at 3:59 PM
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."
Posted by Ben Nadel on Nov 7, 2006 at 4:04 PM
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
Posted by Adam Cameron on Nov 7, 2006 at 6:40 PM
wft is wrong with you? wtf cares? post something useful. You're on goog - dont abuse the privilege with bs rants like this.
Posted by Mike on Nov 7, 2006 at 6:46 PM
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.
Posted by Ben Nadel on Nov 7, 2006 at 6:57 PM
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.
Posted by Mark on Nov 7, 2006 at 9:12 PM
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>
Posted by Dale Fraser on Nov 8, 2006 at 1:06 AM
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! :>
Posted by Peter Boughton on Nov 8, 2006 at 4:48 AM
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.
Posted by Flew on Nov 8, 2006 at 5:08 AM
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')#" />
Posted by Dale Fraser on Nov 8, 2006 at 5:20 AM
@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?
Posted by Ben Nadel on Nov 8, 2006 at 7:32 AM
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.
Posted by Phillip Senn on Nov 8, 2006 at 9:04 AM
Ha ha, nice :)
Posted by Ben Nadel on Nov 8, 2006 at 9:08 AM
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!
Posted by Javier Julio on Nov 8, 2006 at 9:18 AM
Thanks Javier, that means a lot to me :)
Posted by Ben Nadel on Nov 8, 2006 at 9:22 AM
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.
Posted by Matt Williams on Nov 8, 2006 at 10:30 AM
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! :)
Posted by Javier Julio on Nov 8, 2006 at 1:33 PM
Single quotes are better because they use less screen ink.
Posted by Ross on Apr 18, 2007 at 3:03 AM
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.
Posted by Branko Vukelic on Dec 29, 2007 at 10:43 AM
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...
Posted by joran on Jan 14, 2008 at 10:28 PM
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
Posted by Chip on Jan 18, 2008 at 10:29 AM
@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.
Posted by Ben Nadel on Jan 20, 2008 at 10:17 AM
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".
Posted by Xerxes on Apr 2, 2008 at 11:11 PM
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.
Posted by Jeff Martin on May 22, 2008 at 11:56 AM
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.
Posted by Parris on Jul 3, 2008 at 1:29 PM