On 07/06/2017 10:39 AM, Oriol _ wrote:
And why not just use
```js
const result = complexExpression();
const {a} = result;
let {b} = result;
```
and with arrays:
```js
const result = complexExpression();
const a = result[0];
let b = result[2];
```
That's exactly what I do now. It's tolerable, but inelegant. I suspect
it's also not identical semantics, with getters being called different
numbers of times and things, which is mostly irrelevant but could matter
for optimizations. Or maybe destructuring assignment is specced to be
identical, I don't know.
The array ones are pretty ugly. If I'm packing multiple return values
into an array, then the numeric indexes are pure noise.
const [a, _, b] = complexExpression();
is *way* better than
const a = result[0];
const b = result[2];
IMHO, and similarly for objects. Not to mention
const [a, [b, c], d] = complexExpression();
My coding aesthetic is to have as much of the program text as possible
be stuff related to the problem I'm solving, with some amount of
unavoidable wiring (the amount varies widely by language). A temporary
like this requires a name, which with my style implies that it is
significant enough to deserve a name. A reader/reviewer must come to the
realization that it's just a workaround for a language deficiency and
not something semantically meaningful, which is a small speed bump to
understanding. (And in fact, even if the facilities *were* available, I
would at times introduce an intermediate variable anyway, if I felt it
aided understanding of the semantics. The presence or absence would be a
conscious choice based on conveying meaning to the (human) reader, even
though the computer couldn't care less. If there's a good name for the
intermediate, it should probably be there, and if not, it shouldn't.)
In the whole scheme of things, this is minor enough that I wouldn't
personally argue for any special syntax here, but I could support
something if others were interested and it had clean syntax.
If nothing else references `result`, it can be garbage-collected.
In the absence of optimizations (scalar replacement), there's no GC
difference here. complexExpression() is going to create and return a
garbage-collectable object whether you bind it to a name or not. The
optimization is certainly simpler and I would guess much more likely to
be applied if you don't have the temporary, though.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss