Why I use double quotes for strings

NOTICE: PERSONAL OPINION AHEAD (except this is my blog so of course it is personal opinion so why do I even bother to say that. :P)

In pretty much every programming language – I am unaware of one that does not work this way – both single and double quotes create string literals. There are occasions when an API may require a specific quote (the Blender Python API, for example, requires single quotes to work properly) but normally they are interchangeable. Thus which one you use comes down to personal preference.

As I said in the title, I prefer double quotes for my strings literals. In this post I will explain why I do this and some of the exceptions when I do not use them.

I am very good in English (it was one of my better subjects in homeschool), and one of the rules of quoting in English is to use double quotes then single quotes if the quote contains a quote, back to double if that contains a quote, and so on. An example of this would look like so:

In his work Person X wrote “After many years of painstaking work, I confirmed Person Y’s saying ‘I am almost positive Person Z was on the right track when he said “Roses are a perennial flower.” ‘ “

(I bet you were not expecting an English lesson with such a silly example, were you? :P)

I use double quotes all the time not solely because of this rule but because I see string literals almost like quotes so by English convention I would use double quotes, even in parameters (e.g. Isaid("Hiiiiiiiii!!!!"), <table border="0">). The exception to my convention is when

  1. I need a multi-line string
  2. I need to display double quotes in my string
  3. I need to create an HTML parameter

In all three of these situations I use single quotes to construct my string. While the first applies mainly to Python scripting (so far the only way I’ve found to make one in JavaScript is by ending the line with a \ or string concentration with a \n character), the other two are common exceptions I often encounter. So when these exceptions arise my string looks something like this:

'Could not open file "' + myFile + '" for writing'

That is why I use double quotes over single quotes in my code. Remember, this is simply personal preference. I am not trying to convince you of anything, simply stating an opinion. Agree, disagree? Let me know! I’m always open for discussion. ๐Ÿ™‚

-le717

Advertisements

3 thoughts on “Why I use double quotes for strings

    1. True, but in certain scenarios (such as writing an HTML document on-the-fly from within JavaScript), escaping can lead to extra confusion in which quotation mark is escaped and which forms the string. So while escaping is good in some situations, it is not a viable option all the time. Problem partially solved! ๐Ÿ˜›

      1. Not really. That’s just another personal opinion. Personally, I think it’s more readable and concise than using single quotes around double quotes. That depends heavily on font and syntax highlighting though. ๐Ÿ˜›

Triangular Reactions

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s