开发者

Difference using if/else vs just if before a return statement?

开发者 https://www.devze.com 2023-03-29 04:57 出处:网络
I found it开发者_开发问答 hard to search for this question. Will the else statement make any difference in the code below?

I found it开发者_开发问答 hard to search for this question.

Will the else statement make any difference in the code below?

public string Test()
{
  if (statement) return "a";
  else return "b";
}  

What's good practice in this case?


No, it will not make any difference in this case, since if the if-statement is true, it will return and never go to the return "b"; line regardless of the if.

In this case, I usually omit the else, but I don't know if that is what the majority will do in this case.


There is no difference, in fact they both compile down to the same IL:

public bool GetValue(bool input)
{
    if (input) return true;

    return false;
}

IL_0000:  ldarg.1     
IL_0001:  brfalse.s   IL_0005
IL_0003:  ldc.i4.1    
IL_0004:  ret         
IL_0005:  ldc.i4.0    
IL_0006:  ret  

And:

public bool GetValue2(bool input)
{
    if (input)
        return true;
    else
        return false;
}

IL_0000:  ldarg.1     
IL_0001:  brfalse.s   IL_0005
IL_0003:  ldc.i4.1    
IL_0004:  ret         
IL_0005:  ldc.i4.0    
IL_0006:  ret   


The else may be unnecessary, but personally I think it's a small price to pay to add a bit of readability and balance out the if logic.


There is no difference and likely the compiler will emit the same in both cases. As an aside, for small checks like your example you can always use the following (conditional operator):

public string Test()
{
    return statement ? "a" : "b";
}  


It's all about whatever makes the clearest code.

Personally I feel that in most situations the redudant else makes the code clearer,, but others may feel differently.

Either way I would personally be using brackets though for your if/else, but others may feel that this makes there code too verbose.

   public string Test()
    {   
            if (statement)
            {
                 return "a";
            }
            else 
            {
                 return "b";
            }   
    }

or

   public string Test()
    {   
            if (statement)
            {
                 return "a";
            }

            return "b";
    }


No difference, but my preference in this particular case is:

public string Test() {
   return statement ? "a" : "b";
}

In any case, I wouldn't keep the else statement, but that's mostly because I use ReSharper, which dims the else statement, and I'm a little OCD about that sort of thing.


I usually prefer the variant without the else. While they're functionally equivalent, the else version sometimes leads to large number of indents.

public string Test() {
    if (statement)
        return "a";
    return "b";
}

There's nothing really wrong with either variant for a simple case like that, but contrast it with (for example) four checks at the start of a function (pseudocode):

define fn():
    if error condition 1:
        handle error 1
    else:
        if error condition 2:
            handle error 2
        else:
            if error condition 3:
                handle error 3
            else:
                if error condition 4:
                    handle error 4
                else:
                    do something

as opposed to:

define fn():
    if error condition 1:
        handle error 1
        return

    if error condition 2:
        handle error 2
        return

    if error condition 3:
        handle error 3
        return

    if error condition 4:
        handle error 4
        return

    do something

In my opinion, the latter is "cleaner" code.


It will not make any difference. You should write how you like(ofc. stick to one pattern) ;)


Nope ... without the else in the code it will perform the same thing


No. They are logically the same.

 if(xx) return; else return;


 if(xx) return;
 return;
0

精彩评论

暂无评论...
验证码 换一张
取 消