Proposal: Syntax sugar for single exit and early exit functions.
Andreas Rossberg
rossberg at google.com
Mon Nov 17 08:51:49 PST 2014
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
More information about the es-discuss
mailing list