If I have a method that contains a lot of if else statements, for example:
public void myMethod()
{
if(..)
else if(..)
else if(...)
else if(...)
else if(...)
//and so on
}
Would it be faster to place return inside these statements, so that the rest of the code is not checked after one statement is found true, or would it be faster to let the compiler hit the end bracket?
I guess it comes down to the question: Is the return statement efficient or not?
EDIT: The point of the question: Say you have a game loop that is constantly running, with code that holds a lot of methods and 'else ifs'. If I have to update 2000 different objects 60 times a second, that's 120000 'else ifs' checked (at the minimum) per second. So the answer to the question could potentially make a difference over time.
The “return early” pattern is an excellent way to prevent functions from becoming confusing. However, this doesn't mean that it can be applied every time. Occasionally, during the complex business logic, it is inevitable to have some nested “ifs”, even with the option of extracting the code to other functions.
Early return is a situation where the renter returns the rental vehicle before the agreed drop-off time. The supplier may give the renter a refund or charge an Early Return Fee, depending on the supplier's terms and conditions.
In general, a return statement will terminate a loop (that's normally break to just end the loop) by returning flow of control to a method caller; in the case of main() that is the operating system. IMHO the return statement just returns to the caller without "terminating the loop".
No you don't need to use break . It is used to terminate a loop early, but return (within a subroutine) will also terminate any loop it is inside of. Anything that follows return is dead code.
It doesn't matter.
Any sane or insane compiler will produce identical bytecode for either option. (assuming that the code you write is actually equivalent)
You should use whichever approach is more readable.
SLaks' answer is of course correct. But I think the fact that you are asking the question at all shows that you have a misunderstanding about what if-else means. When you say
if(x) X();
else if(y) Y();
else if(z) Z();
return;
That means exactly the same thing as
if(x)
{
X();
}
else
{
if (y)
{
Y();
}
else
{
if (z)
{
Z();
}
}
}
return;
If, say, branch x
is taken then y
is never checked; control jumps straight to the return
after X()
.
In fact, there is no such thing as an "else if" in C# -- or C, C++, Java or JavaScript for that matter. There's just "else"; if you happen to put an if
statement after the else
, that's your business. There's no "else if" any more than this
if (x) X();
else while(y) Y();
is an "else while" statement. You can put any statement you want after an else
; most people choose to put an if
or a block statement, but you can put any statement there you like.(*)
I understand that the else if 'skips', the point of the question was would adding a return make it jump straight out, without taking the time to skip them in the first place. For example, at an extreme, one million if else statements in a row. It still needs to find the end of the line (which could take time). Am I right?
OK, we need to go to an even lower level. An if-else
statement is compiled like this:
if (condition)
Consequence()
else
Alternative();
Continuation();
is actually transformed into a series of goto statements:
if (!condition)
goto ALTERNATIVE;
Consequence();
goto CONTINUATION;
ALTERNATIVE: Alternative();
CONTINUATION: Continuation();
So if you have
if (x) X();
else if (y) Y();
else if (z) Z();
return;
that first becomes
if (x)
X();
else
{
if (y)
Y();
else
{
if (z)
Z();
}
}
which then becomes
if (!x) goto XALTERNATIVE;
X();
goto CONTINUATION;
XALTERNATIVE: if (!y) goto YALTERNATIVE;
Y();
goto CONTINUATION;
YALTERNATIVE: if (!z) goto CONTINUATION;
Z();
CONTINUATION: return;
Does that answer your question?
(*) Except a local declaration; C# forbids that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With