8

Consider these 2 types of implementation:

public int add(int x, int y) {
    return mysteriousAdd(x, y);
}


public int add(int x, int y) {
    int output = mysteriousAdd(x, y);
    return output;
}

A colleague of mine says that second implementation is better because during debugging we get to see the variable that mysteriousAdd returns and it isn't much of an overhead to create that extra variable in the stack. I think the first implementation is better and his point not so valid because today most compilers can show what is the response of the function during debugging without the extra variable and we are also avoiding creation of extra variable in the stack.

Is reference variable creation in the stack a cheap operation? Which of the above 2 methods would you suggest is better for coding and why?

1

2 Answers 2

16

If you compare the assembly/bytecode/IL/etc output of two functions like that, you should find them both to be the same. All but the most bush-league of compilers will optimize out the extra value.

So any performance/memory considerations should be thrown out.

So, make your choice regarding readability. In the example, there is really no value to identifying the return value prior to returning. However, if you are at the bottom of a 20 line calculation or something, it can often be worth it to create the named value before returning, because that name can indicate to a reader information about what the calculation is supposed to result in.

9
  • 4
    I do often create variables that are strictly not necessary, as then it's possible to set a breakpoint and check the value that's about to be returned.
    – Simon B
    Dec 11, 2018 at 17:16
  • 3
    @SimonB, another approach to avoid "debugging variable" is to write tests. Tests will remove 90% of debugging time.
    – Fabio
    Dec 12, 2018 at 3:43
  • @Fabio, except of course when your test fails and you need to debug and then you break out the variable so you can set a break point :D Dec 15, 2018 at 5:19
  • 1
    @Hangman4358 true in 10% of time :) But in tests you will always have a consumer which will assert output result, so I will always catch returned value on test level. And if you have specific tests then you will know what was a reason of failed tests ;)
    – Fabio
    Dec 15, 2018 at 5:59
  • If I have a local I (nearly) always return (because not having it would be more complex), I generally call it result (maybe abbreviated). That name really says it all. Dec 15, 2018 at 14:27
1

Demonstrate in your programming environment that you can read the result of the function call without a variable exactly as easy without the temporary variable. I'm using what I think is quite a good coding environment, and I can't.

So demonstrate it. If you can, your colleague has learnt something. If you can't, you learned something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.