How to explain to a developer that adding extra if - else if conditions is not a good way to "improv

Posted by Lilit on Stack Overflow See other posts from Stack Overflow or by Lilit
Published on 2010-06-09T13:57:24Z Indexed on 2010/06/09 14:02 UTC
Read the original article Hit count: 139

Filed under:
|

Recently I've bumped into the following C++ code:

if (a)
{
  f();
}
else if (b)
{
  f();
}
else if (c)
{
  f();
}

Where a, b and c are all different conditions, and they are not very short.

I tried to change the code to:

if (a || b || c)
{
  f();
}

But the author opposed saying that my change will decrease readability of the code. I had two arguments:

1) You should not increase readability by replacing one branching statement with three (though I really doubt that it's possible to make code more readable by using else if instead of ||).

2) It's not the fastest code, and no compiler will optimize this.

But my arguments did not convince him.

What would you tell a programmer writing such a code?

Do you think complex condition is an excuse for using else if instead of OR?

© Stack Overflow or respective owner

Related posts about Performance

Related posts about coding-style