I would like to use the opportunity to start something that might turn into a small series. I like to call it "WTF Coding". Since I'm "native" to the .NET world, it might also become "WTF# coding" at some time, but for now I'm keeping it open.
What's it about? Simple: WTF. What-The-F***!? As it "WTF were they smoking when they came up with that?"
Let me present you: Issue 1, the "collection initializer improvement in C#"
A newsletter recently announced a blog posting that struck me as interesting: a new way of writing collection initializers in C# 6.0, since the old one was sooooo bad (reference here,
blog, I'm quoting the version of April 14th 2015 in case it's gone and I have it on file)
The blog says: "...gives you a new syntax that saves a few keystrokes but, more importantly, makes the resulting code easier to read by eliminating some curly braces and leveraging the equals sign." - OK... let's compare the two versions:
Old version
Dictionary cList = new Dictionary()
{
{"A123", new Customer("A123")},
{"B246", new Customer("B246")}
};
New version
Dictionary cList = new Dictionary()
{
["A123"] = new Customer("A123"),
["B246"] = new Customer("B246")
};
OK... so... how is that saving me keystrokes??? Let's dig deeper, just so you know what I mean. Cutting out the code that is 1:1 the same it leaves:
{"A123", new Customer("A123")},
{"B246", new Customer("B246")}
vs.
["A123"] = new Customer("A123"),
["B246"] = new Customer("B246")
And then, only comparing one of these to the new version directly and on top of each other:
{"A123", new Customer("A123")},
["A123"] = new Customer("A123"),
Whoops... the new version is even longer by one character. Granted it's a whitespace that would be cosmetics but still: the number of functional characters is EXACTLY the same as before.
The new vesion might be better to read to newcomers as it more closely resembles the object initializers, a comparison that the blog post never even did, but overall I say it's more a question of taste than actual hard facts of "what is better".
What would be more interesting to me: does the new version also call a method "Add" on the initialized object or does it actually use the indexer? That is tech-info that I'd expect in a programmer's blog because that could break code. And if the new version calls "Add" to be compatible... it's wonky and misleading. So: Who came up with that change and when will that person be potty trained? Because it's childish changes like these that make our lives harder than necessary...