A Modest Proposal about PowerShell Strings

If you’ve used the PowerShell Script Analyzer before you are probably aware that you shouldn’t be using double-quoted strings if there aren’t any escape characters, variables, or subexpressions in them.  The analyzer will flag unnecessary double-quotes as problems.  That is because double-quoted strings are essentially expressions that PowerShell needs to evaluate.

Let me repeat that…

Double-quoted strings are expressions that PowerShell needs to evaluate.

Single-quoted strings, on the other hand, are just strings.  There’s nothing (aside from doubled single-quotes being replaced by a single single-quote) that needs to be done with them.

When I teach about PowerShell, I usually say something along the lines of “double quotes are an indication to PowerShell that there’s some work to do here”.  I was thinking about this the other day and I think a shift in terminology will be helpful.  Calling them double- and single-quoted strings is descriptive, but not very helpful.

My proposal is simply this:

Single-quoted strings will henceforth be called “strings”.

Double-quoted strings will henceforth be called “string expressions”.

“String expression” gives me the idea that it is going to be evaluated.  Which fits double-quoted strings perfectly.  It’s also shorter than saying “double-quoted string”, which is a bonus.

“String” sounds (to me, at least), like something static.  Matches the situation again.

For those that think “why not just use double-quoted strings all the time?”, I would counter: Would you use Resolve-Path against C:\?  Probably not, because it would be a waste of time.  There’s nothing to do.  Resolve-Path expands wildcards and there are no wildcards there.  I guess you could use Resolve-Path with every path just to be safe…but you get the point.

What do you think of this proposal?  Do “string” and “string expression” convey enough that they should be used?  Let me know in the comments.

FWIW, I’m going to be using them whether you agree.  🙂

 

–Mike

4 Comments

  1. Pingback: A Modest Proposal about PowerShell Strings - PowerShell Station - How to Code .NET

  2. Pingback: Dew Drop - June 28, 2018 (#2755) - Morning Dew

  3. I wholeheartedly agree! You could go a bit further and say that single quoted strings are “string literals”. This is a carry over concept that some folks used to adhere to in PERL.

    Thanks for the article! I’m tuned in to the station now 🙂

  4. a nit-pick: Do you know where the term “modest proposal” comes from?
    It’s generally used to propose something quite outrageous, to subtly suggest people do the exact opposite. (in this context, something like proposing using Resolve-Path on all strings)

Comments are closed.