Proposal: result-forwarding ternary operator

Naveen Chawla naveen.chwl at gmail.com
Wed Sep 20 13:22:14 UTC 2017


As your example shows, for loops can be reduced to array reduction function
calls. `if` `else`s can be represented by ternaries.

`while` loops aren't so straightforward but can be factored into single
function calls. They are less common anyway.

I find single expressions listed as `(a, b, c)` more readable than code
blocks who eventually evaluate to a single value each.

So, even if `do` expressions were introduced into ES, I would avoid using
them as much as I could, unless it were really smarter and more readable to
use them than any of the alternatives

On Wed, 20 Sep 2017 at 16:11 T.J. Crowder <tj.crowder at farsightsoftware.com>
wrote:

> On Wed, Sep 20, 2017 at 11:28 AM, Naveen Chawla <naveen.chwl at gmail.com>
> wrote:
> >
> > The comma operator seems to make the `do` concept redundant, at
> > least for me.
>
> No, not at all. Again: With the comma operator, you can't use
> *statements*, only expressions. With the `do` expression, you can use
> actual statements:
>
> ```js
> const x = do {
>     for (const x of getTheThings()) {
>         if (x.id == something) {
>             x.foo;
>         }
>     }
> };
> ```
>
> ...which is basically:
>
> ```js
> const x = getTheThings().find(x => x.id == something).foo;
> ```
>
> ...except it doesn't throw if `find` returns `undefined`, and isn't a
> series of function calls.
>
> `do` expressions are basically to address overly-complex conditional
> expressions and IIFEs.
> More: https://gist.github.com/dherman/1c97dfb25179fa34a41b5fff040f9879
>
> -- T.J. Crowder
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170920/833aa08a/attachment-0001.html>


More information about the es-discuss mailing list