On style guides and quotes

Comments None

In JavaScript we have two kind of quotes at our disposal for strings. Most style guides either favors single quotes or implies no limitations but consistency. One might come up with a lot of arguments for each side.

To be honest all of them are either far-fetched or have corresponding counter-arguments. Pieces of HTML as well as messages (which often have single quotes) should mostly reside in script-blocks or separate files and making extra escape for a few strings in the whole app won't work one's fingers to the bone. After all, it's 2015 now, we can use es6 template strings!

There's a misconception I'd like to point out here: "(single|double) quotes eliminate the need to escape (double|single) quotes". Nope! Most strings can have both types: '27" apple cinema display', "levi's jeans". Message can have both single quotes as apostrophe as well as quotes for Press "escape 'em all" button to achieve the desired effect. This is mostly related to generation of HTML as strings, which happens more often on back-end, but nonetheless.

So, to the main point. What I think is that using only one type of quotes is somewhat limiting. I've spontaneously come to the following usage of quotes in JavaScript. In short, I prefer single quotes in cases where options are limited. A notable example is event types. There are only a few dozen event types: click, submit and so on. Semantically those can be considered as an enumeration. On the other side there are strings where the number of combinations is virtually limitless. Two things, which come to mind are inline text resources (logging messages are OK in that role) and mini-templates.

Another interesting idea by @medikoo how to tap into having two types of quotes in the language from the article I mentioned above:

Single quotes for internal strings and double for external.

So mostly people prefer what I'd call "cheap-and-dirty" solution: use only one type to reduce cognitive load, but this isn't the only answer to this question. What do you think? Will adding semantic to different types of quotes and using both of them simultaneously pay off?



There are currently no comments on this article.


Enter your comment below. Fields marked * are required. You must preview your comment first before finally posting.

← Older Newer →