Skip to content

Using single quotes around strings that don't require expansion/evaluation #63

Closed

Description

@Mikecom32

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:

$testVariable1 = 'This is a plain string.'
$testVariable2 = 'This is a string containing a $variable I don't want evaluated'
$testVariable3 = "This is a string containing a $variable I want evaluated"

as opposed to:

$testVariable1 = "This is a plain string."
$testVariable2 = 'This is a string containing a $variable I don't want evaluated'
$testVariable3 = "This is a string containing a $variable I want evaluated"

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:

Activity

  1. dlwyatt commented on Jul 14, 2016

    @dlwyatt

    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.

  2. Jaykul commented on Jul 14, 2016

    @Jaykul
    Member

    The performance argument is simply incorrect.

    Scenario         AverageSeconds 
    --------         -------------- 
    Single-10,000             0.017 
    Double-10,000             0.015 
    Single-1,000,000          1.403 
    Double-1,000,000          1.391
    

    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).

  3. rkeithhill commented on Jul 14, 2016

    @rkeithhill

    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 don't - I'd probably switch to a dq string. I do the same thing with XML attribute values which accept both sq and dq strings. I use whichever is most convenient if there are quote chars in the string.

  4. mattmcnabb commented on Jul 14, 2016

    @mattmcnabb
    Contributor

    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.

  5. Mikecom32 commented on Jul 14, 2016

    @Mikecom32
    Author

    @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.

  6. Jaykul commented on Jul 15, 2016

    @Jaykul
    Member

    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 😀

  7. RamblingCookieMonster commented on Jul 23, 2016

    @RamblingCookieMonster

    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!

  8. locked and limited conversation to collaborators on Jul 22, 2023
  9. converted this issue into a discussion #173 on Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions