In a debate regarding return variables, some members of the team prefer a method to return the result directly to the caller, whereas others prefer to declare a return variable that is then returned to the caller (see code examples below)
The argument for the latter is that it allows a developer that is debugging the code to find the return value of the method before it returns to the caller thereby making the code easier to understand: This is especially true where method calls are daisy-chained.
Are there any guidelines as to which is the most efficient and/or are there any other reasons why we should adopt one style over another?
Thanks
private bool Is2(int a)
{
return a == 2;
}
private bool Is3(int a)
{
var result = a == 3;
return result;
}
result
before you return it.stloc.0
andldloc.0
in the second version). But I think that happens only in Debug mode. And it's not really important here anyway.a = b = c;
anda == b == c
, I would avoid writing something that looks likea = b == c
if you can. When I first saw a line of code like that, it took me a few seconds to figure out what is going on. That code stood out. I would want to slap parenthesis arounda == 3
, but StyleCop does not like it - a good reason to use version number one. Something else: this is essentially a lambda, such asa => (a == 3)
. Why add a line of code to an already bloated trivial function?