3 ms·
I 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:
by Jtsummers 7y ago
I 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 7y agoWhat I really want is nominal destructuring, not positional. It's really fantastic in ES6/TypeScript.
- seemslegit 7y 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.