PiousVenom
PiousVenom

Reputation: 6908

Benefit of having variable assignment on the return line

As stated, I'm curious if there's any benefit of doing:

return someNumber = CalculateResults(parameter);

versus doing:

someNumber = CalculateResults(parameter);
return someNumber;

or doing:

return CalculateResults(parameter);

Is there any performance gained in using one over the other?

Upvotes: 0

Views: 164

Answers (4)

e_ne
e_ne

Reputation: 8459

While with active optimizations probably all the three would be the same, they produce slightly different results without.

The first and the second have to declare a variable in the scope and return it, while the last doesn't. I've tried to compile the following program:

int Calculate()
{
    return 42;
}

int FirstCase()
{
    int x;
    return x = Calculate();
}

int SecondCase()
{
    var x = Calculate();
    return x;
}

int ThirdCase()
{
    return Calculate();
}

Output with LINQPad:

FirstCase:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  call        UserQuery.Calculate
IL_0007:  dup         
IL_0008:  stloc.0     // x
IL_0009:  stloc.1     // CS$1$0000
IL_000A:  br.s        IL_000C
IL_000C:  ldloc.1     // CS$1$0000
IL_000D:  ret         

SecondCase:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  call        UserQuery.Calculate
IL_0007:  stloc.0     // x
IL_0008:  ldloc.0     // x
IL_0009:  stloc.1     // CS$1$0000
IL_000A:  br.s        IL_000C
IL_000C:  ldloc.1     // CS$1$0000
IL_000D:  ret         

ThirdCase:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  call        UserQuery.Calculate
IL_0007:  stloc.0     // CS$1$0000
IL_0008:  br.s        IL_000A
IL_000A:  ldloc.0     // CS$1$0000
IL_000B:  ret         

The third case is a little shorter. I also prefer that approach, because it feels more fluent than allocating a variable for it. The only reason I'd use a variable is to try to "document" what the method does by giving a meaningful name to the return value, through the variable.

Upvotes: 2

Aniket
Aniket

Reputation: 1376

I prefer this

return someNumber = CalculateResults(parameter); 

when possible. Saves a line and helps when you're debugging. You can simply hover over someNumber to get what the return value is.

Performance gain if any will be negligible from not doing an assignment operation. (Compiler will probably optimize it anyway.)

Upvotes: 0

LiquaFoo
LiquaFoo

Reputation: 1721

Having on separate lines will help with debugging like Jacob stated. If you do not need to store/manipulate the data being returned then you should go ahead and return without allocating a variable.

Upvotes: 1

There's no practical benefit between the first two, but the second one is more readable than the first in my opinion.

return CalculateResults(parameter);

is even better if someNumber doesn't persist in any way, you should be careful of that.

and takes one less instruction(although i wouldn't be surprised if the optimizer did that for you already)

Upvotes: 1

Related Questions