10 ms·
This actually seems to be somewhat of a trend in C# as of late. Ever since they introduced lambdas, really. While I use them every day, my favorite is still “r
by chrismeller 8y ago
This actually seems to be somewhat of a trend in C# as of late. Ever since they introduced lambdas, really.
While I use them every day, my favorite is still “replacing” String.Format with string literals. Shorter, sure, but less intuitive particularly for people coming from another language. You can do roughly the same thing in PHP with backticks, but most people avoid them for the same reasons.
I’m not sure how I feel about the trend as a whole. As long as I can continue using the old methods as well I suppose I don’t have much room to complain, but sometimes it does feel like we’re just changing for the sake of change and that’s usually a bad indicator.
- maxxxxx 8y agoI think string interpolation is one of the best things they did to C#. String.Format is pretty error prone.
- kolbe 8y agoI'm on your side. I've found very little use for most improvements since C# 4, but I always use literals now. Things like value tuples and string literals are huge improvements to my code's readability and flow.
- scarface74 8y agoString interpolation is more intuitive than trying to match a placeholder in the string with a list outside of the string.
- chrismeller 8y agoI suppose that depends upon where you’re coming from. Many, many languages have an sprintf-style construct of one form or another, so I think it’s actually much more familiar to most people to have a placeholder with an optional formatter. To each their own, though.
- deleted 8y ago[deleted]
- nicoburns 8y ago> Many, many languages have an sprintf-style construct of one form or another, so I think it’s actually much more familiar to most people to have a placeholder with an optional formatter Many languages also have format strings with interpolation: JavaScript, Python, Ruby, Swift, PHP, etc.
- maxxxxx 8y agoSprintf is very error prone. Maybe people are used to it but string interpolation is clearly superior because it removes a lot of potential errors. With the trend towards safer languages sprintf and similar techniques need to go away.
- oblio 8y agoWell, let's take the TIOBE index as a rough indicator of popularity: https://www.tiobe.com/tiobe-index/ https://www.tiobe.com/tiobe-index/ Java, C/C++/Objective-C/Go (I'm lumping them up because they're basically C + C NextGens), Matlab, SQL, ASM don't have them. Everything else, has them. On the list of languages that don't have it, Java is planning to add it, I don't see the C family to change because it's very conservative, and the others are domain specific and don't care that much about string operations.
- Someone1234 8y agostring interpolation isn't simply a wrapper around String.Format(); it also offers compile time checking which solved a real limitation of String.Format() while offering better performance than concatenation. For example: var h = "Hello "; var w = "world"; var output = String.Format("{1} {2}", h, w); output = $"{h} {w}"; Would compile fine but throw on the String.Format() line due to the typo.
- chrismeller 8y agoI wasn’t necessarily saying it doesn’t offer any benefit, more that it’s not really a benefit that seems to benefit a lot of people. Between code intelligence tools and actually running the code you write (not even in an actual QA process, but simply executing each line) that just doesn’t seem like a big deal to me. There are also instances where, such as with Console.Write you are now formatting a string in order to pass it into... a method that formats strings. Yes, the same benefit is still provided, but with a fairly obvious decrease in clarity and increase in redundancy.
- Someone1234 8y ago> fairly obvious decrease in clarity You feel that "{0} {1} {2}" provides better clarity than $"{FirstName} {MiddleInitial} {LastName}"?
- EpicEng 8y ago>a fairly obvious decrease in clarity In what's ways specifically? Using String.Format you have to keep your indices correct and 'compiling' the string in your head requires jumping back and forth between the format string and the args. Interpolation wins on every account as far as I'm concerned.
- martindevans 8y agoString.Format("Is {0} to read {2} {1} of the {3} out of place", "harder", "half", "because", "string is").
- chrismeller 8y agoIronically that’s actually also one of the key advantages of it. If you have a long string with many placeholders in it and need to add a new one in the middle... you just add it and don’t have to figure out where in the list of params to insert the new arg.
- moomin 8y agoNot so nice for the next guy, though.
- contravariant 8y agoThis is not much of an advantage with respect to string interpolation as you could just as easily put your new value in a (nicely named) variable and insert it wherever.
- EpicEng 8y agoYes, God forbid the variable appear more than once.
- wongarsu 8y ago> but less intuitive particularly for people coming from another language Maybe for people coming from C++, but string interpolation is a feature in Python, JavaScript, PHP, etc. It's not exactly an unusal concept (like it was back when PHP started doing it)