Should I use return/continue statement instead of if-else?
The compiler will almost certainly generate the same code. Even if it didn't, the difference will be probably irrelevant. Hence, the relevant argument is certainly how people would read it.
Therefore the question is how similar "//do some stuff" and "do other stuff" are. If they are conceptually similar, use if/else. If they're conceptually different, use continue/return.
Using 'return' instead of 'else' in JavaScript
I always go with the first method. Easier to read, and less indentation. As far as execution speed, this will depend on the implementation, but I would expect them both to be identical.
It is more efficient to use if-return-return or if-else-return?
Since the return
statement terminates the execution of the current function, the two forms are equivalent (although the second one is arguably more readable than the first).
The efficiency of both forms is comparable, the underlying machine code has to perform a jump if the if
condition is false anyway.
Note that Python supports a syntax that allows you to use only one return
statement in your case:
return A+1 if A > B else A-1
Why we use if, else if instead of multiple if block if the body is a return statement
if-elseif-else
statements stop doing comparisons as soon as it finds one that's true. if-if-if
does every comparison. The first is more efficient.
Edit: It's been pointed out in comments that you do a return
within each if
block. In these cases, or in cases where control will leave the method (exceptions), there is no difference between doing multiple if
statements and doing if-elseif-else
statements.
However, it's best practice to use if-elseif-else
anyhow. Suppose you change your code such that you don't do a return
in every if
block. Then, to remain efficient, you'd also have to change to an if-elseif-else
idiom. Having it be if-elseif-else
from the beginning saves you edits in the future, and is clearer to people reading your code (witness the misinterpretation I just gave you by doing a skim-over of your code!).
Should a function have only one return statement?
I often have several statements at the start of a method to return for "easy" situations. For example, this:
public void DoStuff(Foo foo)
{
if (foo != null)
{
...
}
}
... can be made more readable (IMHO) like this:
public void DoStuff(Foo foo)
{
if (foo == null) return;
...
}
So yes, I think it's fine to have multiple "exit points" from a function/method.
Why is else rarely used after if x then return?
In my experience, it depends on the code. If I'm 'guarding' against something, I'll do:
if (inputVar.isBad()) {
return;
}
doThings();
The point is clear: If that statement is false, I don't want the function to continue.
On the other hand, there are some functions with multiple options, and in that case I would write it like this:
if (inputVar == thingOne) {
doFirstThing();
} else if (inputVar == secondThing) {
doSecondThing();
} else {
doThirdThing();
}
Even though it could be written as:
if (inputVar == thingOne) {
doFirstThing();
return;
}
if (inputVar == thingTwo) {
doSecondThing();
return;
}
doThingThree();
return;
It really comes down to which way most clearly shows what the code is doing (not necessarily which bit of code is shortest or has the least indentation).
Related Topics
Deserialize JSON to Anonymous Object
How to Compare Two Objects in Unit Test
Use Decimal Values as Attribute Params in C#
Dynamically Select Columns in Runtime Using Entity Framework
Creating Simple C++.Net Wrapper. Step-By-Step
When Should I Use Out Parameters
How to Check That a Uri String Is Valid
Linq: How to Exclude Condition If Parameter Is Null
Are Get and Set Functions Popular with C++ Programmers
ASP.NET Core Disable Authentication in Development Environment
Detecting Symbolic Links and Pipes in Mono
Setup Database (Sqlite) for Unity
How to Write Out a Text File in C# with a Code Page Other Than Utf-8
Openssl.Net Porting a Ruby Example to C# (From Railscasts 143 Paypal-Security)