6

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?

  • 2
    Similar question for C#: softwareengineering.stackexchange.com/questions/141711/…, almost a duplicate. – Doc Brown Dec 11 '18 at 17:17
  • 1
    @DocBrown I've searched a dozen times but couldn't get an answer. Even the question heading in the link you posted doesn't have sensible content, but the qn is so similar to the one I asked. How did you scout the duplicate :). – kai Dec 11 '18 at 17:31
  • 2
    Well, it was the first link showing up after searching this site using the keywords "debugging return variable" ;-) – Doc Brown Dec 11 '18 at 17:56
  • 1
    Such search skills, much wow. – kai Dec 11 '18 at 18:00
14

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.

  • 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 '18 at 17:16
  • 2
    @SimonB, another approach to avoid "debugging variable" is to write tests. Tests will remove 90% of debugging time. – Fabio Dec 12 '18 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 – Hangman4358 Dec 15 '18 at 5:19
  • @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 '18 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. – Deduplicator Dec 15 '18 at 14:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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