Proposal: Syntax sugar for single exit and early exit functions.

Jeremy Martin jmar777 at gmail.com
Mon Nov 17 09:00:48 PST 2014


`validate: { ... }`

Is that an ES7 feature? I don't recognize it from ES6, and can't seem to
find a relevant proposal with that syntax...

On Mon, Nov 17, 2014 at 11:51 AM, Andreas Rossberg <rossberg at google.com>
wrote:

> On 17 November 2014 17:29, Alex Kocharin <alex at kocharin.ru> wrote:
> > 17.11.2014, 03:07, “Biju” bijumaillist at gmail.com:
> >> I wish, I could write elegant two of the code pattern I use frequently.
> >> [...]
> >> Patten 2.
> >> I do like to code functions with early exit, like
> >>
> >> function someValidationProcess(){
> >>
> >>     doStuff();
> >>     if(condition_1()){
> >>         doCleanUp_1();
> >>         doCleanUp_2();
> >>     }
> >>
> >>     doAnotherStuff();
> >>     if(condition_2()){
> >>         doCleanUp_1();
> >>         doCleanUp_2();
> >>      }
> >>
> >>     doYetAnotherStuff();
> >>     if(condition_3()){
> >>         doCleanUp_1();
> >>         doCleanUp_2();
> >>      }
> >>
> >>     doMoreStuff();
> >>     doCleanUp_1();
> >>     doCleanUp_2();
> >> }
>
> Sounds like you would like to have monad comprehension syntax. :)
>
> > How about a loop?
> >
> > function someValidationProcess() {
> >   do {
> >     doStuff()
> >     if (condition_1()) break
> >
> >     doAnotherStuff()
> >     if (condition_2()) break
> >
> >     doYetAnotherStuff()
> >     if (condition_3()) break
> >
> >     doMoreStuff()
> >   } while(0)
> >
> >   doCleanUp_1()
> >   doCleanUp_2()
> > }
>
> No need for a fake loop:
>
> function someValidationProcess() {
>    validate: {
>      doStuff()
>      if (condition_1()) break validate
>      doOtherStuff()
>      if (condition_2()) break validate
>      // etc.
>    }
>    doCleanUp_1()
>    doCleanUp_2()
>  }
>
> But I'd rather use a separate function for the clean-up.
>
> /Andreas
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>



-- 
Jeremy Martin
661.312.3853
http://devsmash.com
@jmar777
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141117/8b26fc11/attachment.html>


More information about the es-discuss mailing list