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;
}
4 Answers 4
Because I use Resharper with Visual Studio, Ctrl-RV (Or Ctrl-Alt-V, if you use the Resharper/IntelliJ key bindings) turns your first example into your second example. So when I want to debug, I can do that easily enough. And if I forget to put it back then I won't feel bad because Ctrl-RI will put it right back again to make it easier to read.
Seriously, waste your time arguing about more important things. Like where to put your leading braces or spaces vs tabs.
-
5I prefer my debates to be about invisible characters...ChaosPandion– ChaosPandion2012年03月27日 13:23:38 +00:00Commented Mar 27, 2012 at 13:23
-
Great tip, guys! Now we can continually re-factor each other's code far more quickly than we could before. It'll probably save more time than actually discussing it! :)pb01– pb012012年03月28日 08:06:21 +00:00Commented Mar 28, 2012 at 8:06
-
@pdr does that ctrl + RV work only with resharper ? or is it some sort of custom keybind? it does not work for me.Jane Doe– Jane Doe2013年03月17日 12:23:10 +00:00Commented Mar 17, 2013 at 12:23
-
@JaneDoe: I'm stunned to find that it is a Resharper refactoring and that VS doesn't have an equivalent. Answer corrected. Sorry about that.pdr– pdr2013年03月17日 13:47:12 +00:00Commented Mar 17, 2013 at 13:47
-
@ChaosPandion U+200B for the win!Jesse C. Slicer– Jesse C. Slicer2019年08月21日 14:07:09 +00:00Commented Aug 21, 2019 at 14:07
Personally I find the first example easier to read. You can still debug it, by setting a break point in the return statement and adding a == 2
to the watch window or by using quick watch.
But this is really a matter of personal preference. Both versions are OK.
-
9+1 righting harder to read code to make placing break points easier is doing things the wrong way round imhojk.– jk.2012年03月27日 13:05:28 +00:00Commented Mar 27, 2012 at 13:05
-
Watch window or intermediate window are not always solutions for this problem, as sometimes the expression requires the thread to run.JustAnotherUserYouMayKnowOrNot– JustAnotherUserYouMayKnowOrNot2013年03月17日 15:33:50 +00:00Commented Mar 17, 2013 at 15:33
-
@JustAnotherUserYouMayKnowOrNot: Yes. There is also the possibility to print a message into the debug window from within a breakpoint. Right click the breakpoint and select "When Hit...".Olivier Jacot-Descombes– Olivier Jacot-Descombes2013年03月17日 16:26:06 +00:00Commented Mar 17, 2013 at 16:26
-
Also the expression could have side-effects, running it again could result in problems. Better stick with the result var.JustAnotherUserYouMayKnowOrNot– JustAnotherUserYouMayKnowOrNot2013年03月18日 06:53:59 +00:00Commented Mar 18, 2013 at 6:53
-
1Why can’t debuggers just let you see a value when you hover over return already? Seems like it’s right there on the stack.candied_orange– candied_orange2020年01月22日 02:04:10 +00:00Commented Jan 22, 2020 at 2:04
When the code is as easily readable as your example, there is nothing wrong with returning the result of a logical operation such as return a == 2
. However, if the return value is a more complex statement or looks something like
return a > 2? doOptionA().getResult() > makeDecision("greaterThan2") : doOptionB().getResult() == makeDecision("lessThan2");
then you'll want to use variables to store pieces of that first and simplify the return statement, for the sake of readability.
In a simple example such as that, either one is OK.
For more complicated examples, I prefer the second way. That is only becuase it is more readable and others will likely have to maintain the code.
-
Only if there's a better variable name than
result
, which is itself a completely non-descriptive and useless identifier.Alexander– Alexander2019年08月22日 17:19:45 +00:00Commented Aug 22, 2019 at 17:19
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?