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

Biju bijumaillist at gmail.com
Sun Nov 16 16:07:33 PST 2014


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


More information about the es-discuss mailing list