I don't think we need to add any syntax sugar for the examples below.

Answering with examples inline:

17.11.2014, 03:07, “Biju” [email protected]:

 

 

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;
}

 

 

This is already good enough. If you want to handle exceptions, you have to add try..catch.

 

 




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();
}

 

 

If you do the same stuff after each function, some functional programming could help:

function someValidationProcess() {
  ;[doStuff, doAnotherStuff, doYetAnotherStuff, doMoreStuff]
    .forEach(function(fn) {
      fn()
      doCleanUp_1()
      doCleanUp_2()
    })
}

In this case doStuff would have to set a flag instead of condition_1() probably.

 

 




Proposal:
I wish there was some syntax sugar like

function someClickHandler(){
    doStuff();
    doAnotherStuff();
    doYetAnotherStuff();
} finally {
   return false;
}

 

 

Syntax sugar for try..catch? I don’t think we need one of those.

 

 



function someValidationProcess(){

    doStuff();
    breakif(condition_1());

    doAnotherStuff();
    breakif(condition_2());

    doYetAnotherStuff();
    breakif(condition_3());

    doMoreStuff();

} finally {
    doCleanUp_1();
    doCleanUp_2();
}


 

 

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()
}
​
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to