Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Why String Interpolation is a BAD thing

So I’ll set the scene – I was reviewing some code and refactoring it. First thing to do was to go through and take a look at the Resharper comments as they do a good job at tightening up sloppy code. In this instance I came across code that looked a bit like this:

var url = “”;
url += queryParameters;

Of course, this can easily be refactored using string interpolation to

var url = $“{queryParameters}”;

Which looks so much cleaner. Unfortunately this has actually just made my code even worse – I still have this horrible nasty string literal, now with embedded code. Argh, who does this stuff. Next step, factor the string out into a constants, or ideally configuration file.

private const string ServiceUrlTemplate = “{0}”;

and then:

var url = string.Format(Constants.ServiceUrlTemplate,queryParameters);

I’m sure there are a ton of other ways to make this nicer and perhaps more readable but having a string literal with interpolation is not one of them.

Additional Note: If you’re not using Resharper or another equivalent tool, you’re not working effectively. Start using a refactoring tool today and don’t ignore the warnings. Spend time everyday reviewing and improving the code you write

Comments (6) -

  • Richard

    2/20/2017 10:46:50 PM |

    The first one is "bad", because a variable is being mutated, and so to understand the value of the variable you need to essentially run through the mutations in your head. One advantage of interpolation is that the mutation goes, and it's easy to see in a single place how the string comes to have its value. Using a const is the worst option, because the things that contribute to setting the value are moved further apart, meaning that a reader has to look in more places to find all the information, and there is a new direct coupling added to that configuration (it should be injected in, rather than a call out). Why is string.Format, with magic {0} and parameter order better than an interpolation where you get to name the variables so a reader can see the intent?

  • Norm Bell

    2/21/2017 1:22:28 AM |

    In addition to string constants, you could also utilize a string resource file. This approach lends itself nicely to localization.

  • Norm Bell

    2/21/2017 1:24:12 AM |

    In addition to using string constants, you could also use a string resource file, an approach which lends itself nicely to localization.

  • Adrian Edwards

    2/21/2017 2:03:27 PM |

    Better yet, use the System.UriTemplate class for this. String building for URIs is a BAD thing too! :)

  • Skynet6

    2/21/2017 4:04:09 PM |

    This post should not be about why string interpolation is bad but why hardcoding service addresses is bad. Because you might as well use this:
    $"{Constants.ServiceUrlTemplate}{queryParameters}" instead of

  • mgroves

    2/22/2017 4:32:46 AM |

    I don't understand why it's a bad thing and why using {0} is better. You just state that it is. Could use more explanation?

Comments are closed