3

if(A) then if(B)

vs

if(A and B)

Which is better to use and why ?

geckon
  • 8,316
  • 4
  • 35
  • 59
Bayrem Ben Alaya
  • 297
  • 1
  • 3
  • 16
  • 2
    `if (A and B)` looks more clear. For small conditions it is definitely the way to go; if you want to do some stuff if A but not B, then you probably have to split in two blocks. – fedorqui Jun 05 '15 at 10:07
  • `if(A)`-then-`if(B)` is more flexible, depending on expression length you can choose which to take; if it's too long for both, you can split the parts and store them in variables with apropriate names and solve it step by step. – Binkan Salaryman Jun 05 '15 at 10:10
  • what if i have n conditions, which one is better in that case ? – Bayrem Ben Alaya Jun 05 '15 at 10:10
  • 1
    @BayremBenAlaya: "Better" depends on more information than is present here. Sometimes one version is more clear, sometimes another is more clear. Often with more complex conditionals some refactoring is necessary to make the intent more easily understood, extracting the condition itself into a method or inverting some conditions to prevent excessive nesting. With the contrived example presented, there's little difference. – David Jun 05 '15 at 10:12
  • 2
    Be aware that the semantics are different in languages with side effects. Depending whether the language allows short circuiting conditions! – Alexander Oh Jun 05 '15 at 10:51

3 Answers3

5

Given:

if (a) {
  if (b) {
    /// E1
  }
  /// E2
} else {
  // E3
}

One may be tempted to replace it with:

if (a && b) {
 /// E1
} else {
 // E3
}

but they are not equivalent(a = true and b = false shows the counter-argument for it)

Other than that, there is no reason not to chain them if the language allows short circuits operations like AND, OR. And most of them allows it. Expressions are equivalent and you can use the chained form to improve code readability.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
4

It depends on your specific case but generally:

1) if (A and B) looks better/cleaner. It's immediately clear that the following block will execute if both A and B apply.

2) if(A) then if(B) is better in cases when you want to do something also when A applies, but B doesn't. In other words:

if (A):
    if (B):
        # something
    else:
        # something else

generally looks better than

if (A and B):
    # something
if (A and not B):
    # something else
geckon
  • 8,316
  • 4
  • 35
  • 59
2

You've tagged this 'algorithm' and 'logic' and from that perspective I would say little difference.

However in practical programming languages there may or may not be a question of efficiency (or even executability).

Programming languages like C, C++ and Java guarantee that in the expression A && B that A is evaluated first and if false B is not evaluated. That can make a big difference if B is compuatationally expensive or is invalid if A is false.

Consider the following C snippet:

int*x
//....
if(x!=NULL&&(*x)>10) {
//...

Evaluating (*x) when x==NULL will very likely cause a fatal error.

This 'trick' (called short-circuit evaluation) is useful because it avoids the need to write the slightly more verbose:

if(x!=NULL){
  if((*x)>10){

Older versions of VB such as VB6 are infamous for not making the short circuits.

The other one is that B in A || B will not be evaluated if A is true.

Discussion about support:

Do all programming languages have boolean short-circuit evaluation?

In any language that provides short-circuit and has an optimizing compiler you can assume there is unlikely to be any difference in code efficiency and go with the most readable. That is normally if(A&&B).

Community
  • 1
  • 1
Persixty
  • 8,165
  • 2
  • 13
  • 35