4 ms·
Positional records are a terrible idea, one of the best things about C# is readability var (f, l) = person; is so much worse than var (f, l) = (person.FirstN
by seemslegit 6y ago
Positional records are a terrible idea, one of the best things about C# is readability
var (f, l) = person;
is so much worse than
var (f, l) = (person.FirstName, person.LastName);
I don't want to have to refer to documentation or class implementation to understand what a destructuring expression does.
- jonesetc 6y agoI think that it's unfortunate they stuck with that example instead of the classic point class example for positional args. In general the positional destructuring is not great, but there are some cases where it is nice and readable. Because of that I'm glad it's not the default that all record classes get it. var (f, l) = person; is gross and hard to understand. var (x, y) = point; is simple and readable.
- andybak 6y agoYour example would seem a lot less bad without the one character variable names. var (first, last) = person; didn't seem too bad to me.
- Jtsummers 6y agoI disagree. Automatic destructuring like this can be fantastic. Sure, if you're just destructuring a variable there's not much gain. But this is much smoother: var (f, l) = SomeMethodReturningAPerson(); than: var person = SomeMethodReturningAPerson(); var (f, l) = (person.FirstName, person.LastName); or: foreach(var (f, l) in enumerationOfPersons) { ... } compared to: foreach(var person in enumerationOfPersons) { var (f, l) = (person.FirstName, person.LastName); ... } When you know what you want to compute, but have to throw in extra temporary variables it becomes tedious and error prone. Cut straight to the thing you want (the names themselves in this case), eliminate middlemen.
- moogly 6y agoWhat I really want is nominal destructuring, not positional. It's really fantastic in ES6/TypeScript.
- seemslegit 6y agoYou are aproaching this from the mindset of the code author, but consider a new contributor to a project reading for the first time, with positional destructuring foreach(var (f, l) in enumerationOfPersons) { ... } tells him absolutely nothing about what's going or even that there are names involved foreach(var (firstName, lastName) in enumerationOfPersons) { ... } is better but doesn't give him an object to inspect to see where they come from and what else is available.
- contextfree 6y agoNote that, even without positional records, C# already allows types to support this syntax: https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct (the "Person" type would have to implement a "Deconstruct" method with two string out parameters, or alternatively someone could write an equivalent extension method)