Lenik
Lenik

Reputation: 14458

Should I remove unnecessary `else` in `else if`?

Compare the two:

if (strstr(a, "earth"))     // A1
    return x;
if (strstr(a, "ear"))       // A2
    return y;

and

if (strstr(a, "earth"))     // B1
    return x;
else if (strstr(a, "ear"))  // B2
    return y;

Personally, I feel that else is redundant and prevent CPU from branch prediction.

In the first one, when executing A1, it's possible to pre-decode A2. And in the second one, it will not interpret B2 until B1 is evaluated to false.

I found a lot of (maybe most of?) sources using the latter form. Though, the latter form looks better to understand, because it's not so obviously that it will call return y only if a =~ /ear(?!th)/ without the else clause.

Upvotes: 1

Views: 512

Answers (5)

Stephen C
Stephen C

Reputation: 719561

(The following answers the original version of the question.)

Do you realize that the two code snippets are NOT semantically equivalent???

Consider what happens if a is "earth".

  • The first snippet calls foo() and then bar().
  • The second snippet calls foo() and skips the bar() call.

And this explains why the generated machine code is different. It has to be to implement the different semantics of the respective code fragments!

Personally, I feel that else is redundant ...

Unfortunately, your feeling is incorrect.

Lesson - write your code simply and clearly and leave optimization to the compiler ... which is going to do a far more accurate job than you can achieve.


FOLLOWUP

The snippets in the updated version of the question are now semantically identical, and the else is redundant. However:

  • any half decent optimizing compiler will generate identical code for the two snippets, and
  • it is a matter of opinion (i.e. subjective) which of the snippets is easier to understand.

Upvotes: 3

Lundin
Lundin

Reputation: 215115

Why not simply write

char* str;

strstr(a, "ear")

if (str != NULL)
{
  foo();

  if(strstr(str, "earth") != NULL)
  {
    bar();
  }
}

Upvotes: 0

Alexandre C.
Alexandre C.

Reputation: 56986

Use else if to state your intentions clearly. Code is meant to be read by humans.

Let the compiler optimize this, and don't worry about optimization until your code is 1) working 2) crystal clear 3) profiled (do this in that order). When doing step 3, you'll notice that the bottlenecks are not where you supposed they would be.

Any attempt to control branch prediction or whatever low level stuff is silly: compilers are very good at optimizing and they use sophisticated methods to yield a fast code on your particular machine.

Look at output from LLVM based compilers to see what I mean: sometimes you can't even remotely understand what it does.

Upvotes: 2

ana
ana

Reputation: 11

usually it's better to use the second way if you want to test exactly the condition for a, for the exact solution, to reduce the options for the var or const "a". if you write two separate if's you can get 2 different solutions.

for example in your situation with the exact conditions you have there let's say a= -2

A: if (a < 0)
    return x; // if -2 is less than 0 will return x and it stops.
else if (a < 100)
    return y; // 

B: if (a < 0)
    return x; // -2 is less than 0 so it will return x and passes to the next if statement;
 if (a < 100)
    return y; // -2 is also less than 100 and it will return y too

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994619

Your compiler probably knows that both these examples mean exactly the same thing. CPU branch prediction doesn't come into it.

I usually would choose the first option for symmetry.

Upvotes: 6

Related Questions