Interpolated $trings Were Worth The Long Wait

Let’s skip the “no pun intended” and get to “where has this been all my life?”. I don’t recall ever adapting to a new concept in coding so quickly. Right away I went through the whole engine and rewrote every single Log() message, using the repetition to try and force myself into a new habit.

Probably since VB3 I’ve been in the habit of using the old style of String Concatenation, which is done with Ampersands. It’s been an automatic behavior for me for a long time, so I never got around to String.Format(), which I felt was even more clunky and unintuitive.

Fortunately the geniuses at Microsoft finally found the brilliantly simple solution by going back to the Dollar Sign. In the old days of BASIC it was used to define Strings, but with the many changes in Dot Net, most symbols in VB were abolished. But now we’re back to using them but in better ways, making this the second best use for the cash sign since the invention of buying stuff.

All three of these lines do the same thing, but only one of them is awesome at it.

    '// vbCrLf = Carriage Return + LineFeed (ASC(13) & ASC(0))
    DebugInfo = "Variable 1: " & Variable1 & vbCrLf &
                "Variable 2: " & Variable2 & vbCrLf

    DebugInfo = String.Format("Variable 1: {0}{1}Variable 2: {2}",
                              Variable1, vbCrlf, Variable2)

    DebugInfo = $"Variable 1: {Variable1}{vbCrLf}Variable 2: {Variable2}"

The Curly Braces and the Variables inside of them would be white in Visual Studio, to indicate they’re not interpreted as part of the String.

While this is relatively new, and it’s simple enough that it barely needs it’s own article, I wrote one anyway because it shows you my favorite kind of coding. Whenever you can reduce something down to a concept that is extremely simple and clever, anything based on it is usually improved for it.

Often times it requires thinking in a direction that avoids the usual inclination toward complexity. When you focus on complexity things become complicated. Aim for simplicity, and you get sophisticated. And often it takes going the extra mile to make things easier in the future, like some kind of fourth-dimensional laziness.

Leave a Reply