-3

I have a question about creating optimal C++ program.

I have a function that computes expressions like:

c= a/2
c = (a*b)/2
c = (a/2) + b

etc. is it better to use a variable to store these values or just use return <expression>?

I understand creating variable will take space and return <expression> would avoid that. But if these are several returns, does it create more overhead than declaring a variable?

Jason C
  • 38,729
  • 14
  • 126
  • 182
weu833
  • 1
  • 3
    You may check the assembly code generated with both the cases.. – sjsam Aug 27 '16 at 13:42
  • 5
    It doesn't matter, an optimizing compiler will see through all this and compile to the same code. – Peter G. Aug 27 '16 at 13:42
  • 1
    Look up the phrase "common subexpression elimination". – Cody Gray - on strike Aug 27 '16 at 13:48
  • @CodyGray: or see the exact duplicate I found. I didn't read the answers to see if they were any good, and I'm sure I've seen other questions about using temporaries or not that did get good answers. Anyway, care to apply the dupe-hammer? I'm still 50 upvotes from a C++ gold badge :/ – Peter Cordes Aug 27 '16 at 22:23

3 Answers3

5

This is a typical case of useless micro-optimization. The compiler will most likely optimize the variable away anyways.

A reason for using a variable would be documentation. What I mean is that you can give the variable a meaningful name. But this is not a very good argument because the function name should already express what the function returns.

Therefore in most cases I return the expression without assigning it to a variable first. Each variable you add to your code means additional mutable state which is generally considered to be a bad thing because it adds complexity.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • It *is* a very good argument! – Jason C Aug 27 '16 at 13:43
  • @JasonC: In how far do you think it is more expressive than the function name? The question is about assigning the variable and then directly returning it. – Frank Puffer Aug 27 '16 at 13:46
  • I meant that as a supporting compliment, this is a very good answer. Var name vs fn name, it's not one or the other. Do both. Expressive function names and expressive variable names. For example perhaps that variable you're returning is used all throughout the function body before it is returned. Nice to know what it means! And variable names in general. – Jason C Aug 27 '16 at 13:49
2

It doesn't make any difference whatsoever.

The compiler will perform a number of optimizations that will make any differences go away, getting rid of unused intermediates, optimizing expressions, maybe even just sraight up inlining the function where it's called, etc. Even if it didn't perform those optimizations, there'd be basically.no difference. You're focusing on the wrong things here, higher level optimizations such as choosing appropriate algorithms for your task are are important.

The best thing for you to do here is to write code that is clear, easy to read, and easy to maintain.

Jason C
  • 38,729
  • 14
  • 126
  • 182
0

As said before the compiler will optimize it away anyways, but consider also debugging and maintenance. If you directly return the expression result it could cost you a lot of headaches. Consider a very simple example that your result of your function will determine a size of a vector but your result is negative.

blint587
  • 191
  • 1
  • 10