Article 4EMCP CodeSOD: Interpolat(interpolation)on

CodeSOD: Interpolat(interpolation)on

by
Remy Porter
from The Daily WTF on (#4EMCP)

C# has always had some variation on "string interpolation", although starting in C# version 6, they added an operator for it, to make it easier. Now, you can do something like $"{foo} has a {bar}", which can be a very easy to read method of constructing formatted strings. In this example, {foo} and {bar} will be replaced by the value of variables with the same name.

C#'s implementation is powerful. Pretty much any valid C# expression can be placed inside of those {}. Unfortunately for Petr, that includes the string interpolation operator, as a co-worker's code demonstrates"

string query = $@"SELECT someColumn1, someColumn2, someColumn3 FROM myTableWHERE (TimeStamp >= '{startTime}' AND TimeStamp < '{endTime}'){(filterByType ? $"AND (DataType IN ({filetrOptions.Type}))" : string.Empty)}"; // Continued in this style for few more rows

This interpolated string contains an interpolated string. And a ternary. And it's for constructing a SQL query dynamically as a string. There's an argument to be made that this code is literally fractal in its badness, as it nests layers of badness within itself.

otter-icon.png [Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today! TheDailyWtf?d=yIl2AUoC8zAqC0oMf8ckPs
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments