Should I use return/continue statement instead of if-else?

前端 未结 13 1905
时光说笑
时光说笑 2020-12-01 10:46

In C, C++ and C# when using a condition inside a function or loop statement it\'s possible to use a continue or return statement as early as possible and g

13条回答
  •  感情败类
    2020-12-01 10:58

    The code would be more readable if termination criteria are handled first. I always prefer, checking for conditions that require a break or return rather than those that would need lengthy code execution. I prefer:

     if (termination condn) 
          return;
     // code 
     // code
    

    to

    if (success condn)
    {
      // code
      // code
    }
    else
     return;
    

    This makes reading and understanding the code easier.

提交回复
热议问题