A co-worker submitted code for a PR the other day, which effectively follows this flow:

if (a)
  do something_a
else if (b)
  do something_b
else if (c)
  do something_a

where a, b, and c are mutually exclusive conditions (in reality, they are all checking if a variable is equal to different values).

In my experience however, I've always tried to collapse the conditionals that have the same results with ORs, like the following.

if (a OR c)
  do something_a
else if (b)
  do something_b

Disregarding performance, which version is better? Which version is easier to maintain in the future?


3 Answers 3


At least as the question was originally asked, the two aren't equivalent.

In the first, something_a is executed only if a is true or if b is false and c is true. I.e., if (a || (c && !b)).

In the second, something_a is performed if either a or c is true (regardless of b). Only if those are both false do we consider b.

As such, your first question shouldn't be which is easier to maintain, but which behavior you actually want.

If the conditions are exclusive, so that difference doesn't arise, then I'd consider the relationship between the conditions, if any. If it's basically coincidence that two inputs happen to produce the same output, then I'd probably list each individual, since it's probably easier to simply map from inputs to results when each is listed directly. The second becomes interesting primarily when/if there's a fairly obvious relationship between the conditions.

For example, if you had something like:

if (customer has no ID || customer's age < minimum)
    reject order for alcoholic beverage

In this case, we have two different manifestations of what's really a single condition: the customer can't prove they're old enough to buy the drink. They're not (from our perspective) two unrelated conditions that happen to lead to the same result--rather, they're just different ways of expressing the same condition, so it makes more sense to group them together.

  • The question has now been clarified to make it explicit that a, b and c are mutually exclusive. Commented Jul 25, 2017 at 6:35

Duplication is bad, therefore your first version is inferior.

How much worse it is depends on how much stuff do_stuff_a() actually is; if it's really just one line, then it's no big deal, but repeated groups of statements should generally be combined or refactored into a method.


Neither is 'better' its just coding style.

(Disregarding both performance and whether they have the same result!!!!!)

  • 1
    The first one violates DRY, so they are not equivalent.
    – JacquesB
    Commented Jul 25, 2017 at 8:11
  • 2
    not if its just a function call
    – Ewan
    Commented Jul 25, 2017 at 8:15
  • 1
    Coding style is not just "just" coding style. Different styles have different semantics.
    – Jay Elston
    Commented Jul 25, 2017 at 16:50
  • 1
    no. they literally have the same semantics
    – Ewan
    Commented Jul 25, 2017 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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