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

Frankie Bagnardi f.bagnardi at gmail.com
Sun Nov 16 18:23:24 PST 2014


Returning false isn't the common way to prevent the default action anymore,
event.preventDefault() is.  In that case you'd just preventDefault at the
top of your function.

The other use cases can be satisfied with simple high order functions like
andReturn(f, x) or compose(f, g).

On Sun, Nov 16, 2014 at 5:07 PM, Biju <bijumaillist at gmail.com> wrote:

> I wish, I could write elegant two of the code pattern I use frequently.
>
> Patten 1.
> HTML button click event handler should always return false (ie, when
> you never want the submit action).
> So I always write.
>
> function someClickHandler(){
>   try {
>     doStuff();
>     doAnotherStuff();
>     doYetAnotherStuff();
>   } catch(e){
>   }
>   return false;
> }
>
>
> 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();
> }
>
>
> Proposal:
> I wish there was some syntax sugar like
>
> function someClickHandler(){
>     doStuff();
>     doAnotherStuff();
>     doYetAnotherStuff();
> } finally {
>    return false;
> }
>
>
> function someValidationProcess(){
>
>     doStuff();
>     breakif(condition_1());
>
>     doAnotherStuff();
>     breakif(condition_2());
>
>     doYetAnotherStuff();
>     breakif(condition_3());
>
>     doMoreStuff();
>
> } finally {
>     doCleanUp_1();
>     doCleanUp_2();
> }
>
>
> Cheers
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141116/b53d670f/attachment.html>


More information about the es-discuss mailing list