2

Which format is better in terms of speed, performance and machine code size?

Last return is encapsulated:

static bool MyClass::IsEqual(int A, int B)
{
    if (A == B)
    {
        return true;
    } 
    else
    {
        return false;
    }
}

Last return is not encapsulated:

static bool MyClass::IsEqual(int A, int B)
{
    if (A == B)
    {
        return true;
    }
    return false;
}
10
  • 3
    If the compiler is not braindead, both should give identical code. Sep 19, 2012 at 19:19
  • 1
    Agreed. But if you are unsure compile it into ASM yourself and look at it.
    – BSull
    Sep 19, 2012 at 19:20
  • 1
    Clearly the latter will compile faster because it has fewer characters ;) Of course, the end user doesn't care how long it took to compile.
    – Kevin
    Sep 19, 2012 at 19:21
  • 7
    return A==B ;-) Sep 19, 2012 at 19:21
  • 7
    Seriously, I've seen a lot of code like if(something) return true; else return false; this is bad either way. Sep 19, 2012 at 19:23

4 Answers 4

6

To answer your question - generate and look at assembler code. I bet the assembler code is identical - so no difference in performance.

I always remove if/else if possible, considering your example I'd do:

static bool MyClass::IsEqual(int A, int B)
{
    return (A == B);
}

But I know the real world examples are not as simple as this.

0
5

Which format is better in terms of speed, performance and machine code size?

They should all be identical or close to it. Moreover, you've asked the wrong question.

A better question would be,

Which should I prefer and why?

Bear in mind that C++ code was designed to be read by humans, not machines. Given this, a primary motive for selecting one coding style over another should be how readable it is to humans. As important as this consideration is, it's also unfortunately subjective. The bottom line is you have to decide for yourself which is better, but the important bit is that you're asking the right questions for the right reasons and thought about the answer.

2

Both code snippets should produce the exact same result, so that's not an argument for either of them.

What really decides which version to use is coding style. The if/else variant has the advantage that both branches are on the same logical layer. Sometimes that may be a good reason to write it this way.

2
  • Conceptually it may be that the other case makes more sense in some scenarios, e.g. for checking inputs first and then having the "normal" code path at the least indented level.
    – Flexo
    Sep 19, 2012 at 19:28
  • True but in this case it's just return statements and it's ugly that way. But I agree, for param validation leaving out the else is much cleaner.
    – BSull
    Sep 19, 2012 at 19:32
1

The second form is slightly more readable, because it reduces the nesting level. There is absolutely no performance penalty for using one form or the other.

10
  • 2
    I would disagree. Keeping everything in a scope is more readable to me and also more maintainable. Returning merely (A == B) is the best solution though.
    – BSull
    Sep 19, 2012 at 19:25
  • 1
    I assume that your code is a good deal more complex than what you are showing, so I am commenting only on the additional nesting of the second return statement. In particular, there is no point to use if-then-eles for something as simple as return A == B; Sep 19, 2012 at 19:25
  • 1
    @dasblinkenlight yeah, exactly, and also the 2nd form cannot generate false positives for increased warning levels (which everybody uses, right?) about returning without a value.
    – user529758
    Sep 19, 2012 at 19:27
  • 3
    @BSull That is a rather subjective statement: there are valid arguments for both sides. For example, if your function does error checking of your parameters at the top, and needs to return an error code if one of the parameters is wrong, not using a return statement right there at the top creates a level of nesting that is both unnecessary and misleading. Sep 19, 2012 at 19:41
  • 5
    @BSull - I strongly disagree with your point about return statements. C++ is not C or assembly language
    – Flexo
    Sep 19, 2012 at 19:43

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