Suppose I have an
if statement with a
return. From the efficiency perspective, should I use
if(A > B):
if(A > B):
Should I prefer one or another when using a compiled language (C) or a scripted one (Python)?
With any sensible compiler, you should observe no difference; they should be compiled to identical machine code as they’re equivalent.
Version A is simpler and that’s why I would use it.
And if you turn on all compiler warnings in Java you will get a warning on the second Version because it is unnecesarry and turns up code complexity.
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
Regarding coding style:
Most coding standards no matter language ban multiple return statements from a single function as bad practice.
(Although personally I would say there are several cases where multiple return statements do make sense: text/data protocol parsers, functions with extensive error handling etc)
The consensus from all those industry coding standards is that the expression should be written as:
if(A > B)
result = A+1;
result = A-1;
The above example and the two examples in the question are all completely equivalent in terms of efficiency. The machine code in all these cases have to compare A > B, then branch to either the A+1 or the A-1 calculation, then store the result of that in a CPU register or on the stack.
From Chromium’s style guide:
Don’t use else after return:
return 1 if foo else 2
This is a question of style (or preference) since the interpreter does not care. Personally I would try not to make the final statement of a function which returns a value at an indent level other than the function base. The else in example 1 obscures, if only slightly, where the end of the function is.
By preference I use:
return A+1 if (A > B) else A-1
As it obeys both the good convention of having a single return statement as the last statement in the function (as already mentioned) and the good functional programming paradigm of avoiding imperative style intermediate results.
For more complex functions I prefer to break the function into multiple sub-functions to avoid premature returns if possible. Otherwise I revert to using an imperative style variable called rval. I try not to use multiple return statements unless the function is trivial or the return statement before the end is as a result of an error. Returning prematurely highlights the fact that you cannot go on. For complex functions that are designed to branch off into multiple subfunctions I try to code them as case statements (driven by a dict for instance).
Some posters have mentioned speed of operation. Speed of Run-time is secondary for me since if you need speed of execution Python is not the best language to use. I use Python as its the efficiency of coding (i.e. writing error free code) that matters to me.
I know the question is tagged python, but it mentions dynamic languages so thought I should mention that in ruby the if statement actually has a return type so you can do something like
rv = if (A > B)
Or because it also has implicit return simply
which gets around the style issue of not having multiple returns quite nicely.
From a performance point of view, it doesn’t matter in Python, and I’d assume the same for every modern language out there.
It really comes down to style and readability. Your second option (if-else block) is more readable than the first, and a lot more readable than the one-liner ternary operation.