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

前端 未结 13 1879
时光说笑
时光说笑 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:55

    I had the following in my code:

        while(){
          boolean intersect = doesIntersect(interval_1,interval_2);
          if(!intersect){
             array.add(interval_2);
             if(// another condition){
                // Do some thing here
             }
             continue;
          }
          // other stuff to do if intersect
        }
    

    Was confusing whether I should use continue there or use else but I decided that the inner if condition might make the else not well readable, so I used continue.

    I think readability is what matters!

    0 讨论(0)
  • 2020-12-01 10:56

    Do not sacrifice readability for premature optimization.

    For example:

    void function() {
        if( condition ) {
            //do some stuff
        } else {
            //do other stuff
        }
    }
    

    is in most cases binary equivalent to

    void function() {
        if( condition ) {
            //do some stuff
            return;
        }
        //do other stuff
    }
    

    (i.e. the resulting code is probably the same). But the readability of the former is much better, because you can clearly see that the code will to either X or Y.

    0 讨论(0)
  • 2020-12-01 10:56

    as other people said, only use return/continue if things are short.

    Personally i only use continue if it is possible to write on one line like:

    while( loopCondition ) {
        if( innerCondition ) continue;
    
        //do other stuff
    }
    

    If it's not possible to write it like this without code getting ugly, then if / else.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 11:00

    My personal approach of choosing one is that if the body of the if part is very short (3 or 4 lines maximum), it makes sense to use the return/continue variant. If the body is long, it's harder to keep track of the control flow so I choose the else version.

    As a result, normally, this approach limits the usage of return/continue style to skip some data and avoid further processing rather than process this using one of the following methods (which is better suited by if/else).

    0 讨论(0)
  • 2020-12-01 11:01

    For grins, I did a search across my company's codebase for "continue;" just to get an idea of where it's being used. We use if 695 times across 59 projects in one solution, roughly 1500 source files.

    The main ways I see them being used are as a quick filter:

    foreach (Frobozz bar in foo) {
        if (QuickFilterExclude(bar))
            continue;
        // extensive processing
    }
    

    A recovery from an expected exception:

    foreach (Frobozz bar in foo) {
        Baz result = new Baz(kDefaultConfiguration);
        try {
            Baz remoteResult = boo.GetConfiguration();
        }
        catch (RemoteConnectionException) {
            continue;
        }
        result.Merge(remoteResult);
        ReportResult(result);
    }
    

    And finally in state machinery.

    0 讨论(0)
提交回复
热议问题