2.1 Commutable or not?

Conjunction and disjunction, by their mathematical definitions, are commutable. This means that A B is equivalent to B A. Disjunction has a similar property. This can be easily proven by examining all four combinations of operands for these operators.

However, in programming, conjunction and disjunction are often not commutable. This is because we like programs to execute as quickly as possible. Now, what does that have to do with whether the operators are commutable?

Let us consider the boolean expression AB. As a program evaluates this expression, let’s say that A evaluates to false. Because of the nature of conjunction, we already know that the entire express is false without needing to evaluate B. As a result, many programming languages specify that when the left operand (component, parameter) of conjunction is false, the right operand is not evaluated.

A similar strategy applies to disjunction. In the case of disjunction, if the left operand is true, then the whole disjunction must be true. Consequently, if the left operand of disjunction is true, many programming languages specify that the right hand side is skipped.

This “optimization” to save execution time makes the two operators not commutable any more. In other words, the position (left or right) of operands is important for conjunctions and disjunctions.