On 17 November 2014 17:29, Alex Kocharin <a...@kocharin.ru> wrote:
> 17.11.2014, 03:07, “Biju” bijumaill...@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@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to