Replies: 7 comments
-
I don't really see any difference in readability. It does help to avoid bugs on occasion, though. I use this practice myself, but I don't stress about it if I'm working on a project where someone else has double-quoted string literals without any expansion. |
Beta Was this translation helpful? Give feedback.
-
The performance argument is simply incorrect.
The argument about readability not convincing. Especially when your example contains a perfect counter-example: the word "don't" ... your code is broken. Given that double-quoted strings are faster, and that readability in this case is in the eye of the beholder, I don't see any reason to make a language-wide best practice recommendation here. As always, individual authors, companies, or projects may want to be more strict and take a stance one way or the other. One might, for instance, want to forbid single-quoted strings and require using double-quotes for everything, with escapes where necessary -- to avoid needing to switch back and forth just because you decide to put a variable in or take one out, and prevent problems like "don't" which crop up when people use things that are unfamiliar. Alternatively, one might simply forbid using expressions in strings, and require single-quoted strings everywhere, and the use of string formatting to embed calculated values (like C# prior to 6.0). |
Beta Was this translation helpful? Give feedback.
-
I think there is a "bit" of readability, or perhaps scan-ability, improvement when using a sq string. If I see one of those I know I can quickly scan past it, confident I don't have to eval any subexpression script. Hey, the string might be really long and scrolled out of view in my text editor. It could happen. :-) That said, for something like |
Beta Was this translation helpful? Give feedback.
-
I like Joel's point about maintaining a script down the road. I've probably lost a lot of time writing scripts when I need to go back and change single quotes to double quotes in order to include a variable expansion. If all strings were simply double-quoted then I could shave some time off of script maintenance. I've switched to this practice recently, but for your own work you may need to weigh it's benefits against the possibility of introducing a bug. Of course this should be prevented by using a good editor with the right syntax highlighting. |
Beta Was this translation helpful? Give feedback.
-
@Jaykul The performance reason was something I was quoting from the two blogs I linked above. I haven't tested it myself. Either way, it's incredibly minor. To be fair, the issue with quotes in strings crop up with both conventions. It's pretty funny that I screwed it up myself freehanding into the github text box. I think it is/was worth discussing at least. |
Beta Was this translation helpful? Give feedback.
-
Yeah, it's definitely a question that comes up, and the difference between single and double and here-strings is something we should make sure new scripters understand. I don't think we can make a recommendation that a certain way is definitely better, but this conversation about it is definitely worth having and keeping a log of 😀 |
Beta Was this translation helpful? Give feedback.
-
I'm all for espousing the benefits and caveats (slight performance impact even at massive scale, subjective readability), but I'm not sure if this fits as a good practice, let alone a best practice. On the other side, I can't tell you how much time I've wasted using single quotes and having to go back and switch to double quotes. Cheers! |
Beta Was this translation helpful? Give feedback.
-
I searched and didn't see a previous issue for this. Do you think we should recommend using single quotes around any string that doesn't require some type of evaluation?
I'd propose that if a string is just a string, it should be surrounded by single quotes:
as opposed to:
I think this improves readability and is more consistent. Also, while the difference is very minimal, single quoted strings generally perform faster than double quoted strings when there is no expansion/evaluation.
A bit more info:
Beta Was this translation helpful? Give feedback.
All reactions