In programming languages that have short-circuit operators, order the conditions to be tested according to their probability of fulfillment.
A short-circuit operator is a conditional operator for which the evaluation of its second operand can be omitted according to the result of the evaluation of its first operand (the value of the operation could be entirely determined according to the value of the first condition). Conversely, an operator that does not short-circuit will necessarily evaluate its two operands to determine the value of its operation. In C, C++, C#, Java, JavaScript, or PHP, for example, the logical operators && (AND) and || (OR) are short-circuit operators. In the case of the operator &&, for example, during the evaluation of an expression like Expression = ConditionA && ConditionB, if ConditionA evaluates to false, then it is not necessary to evaluate ConditionB, and Expression evaluates to false. In the case of the || operator, during the evaluation of an expression like Expression = ConditionA || ConditionB, if ConditionA evaluates to true, then there is no need to evaluate ConditionB, and Expression evaluates to true.
Given the special behavior of short-circuit operators and in order to take advantage of them to limit the number of operations executed in a program, and thus reduce energy consumption, we recommend writing the operations using these operators by placing the condition most likely to cause the short-circuit (the non-evaluation of the second operand) as the first operand. So, to use the example of the short-circuit type operators && and || found in C, C++, C#, Java, JavaScript, or PHP, if condition ConditionA is more frequently evaluated as false than condition ConditionB, then we will choose to write in the code ConditionA && ConditionB rather than ConditionB && ConditionA; in this scenario also, we will choose to write ConditionB || ConditionA rather than ConditionA || ConditionB.
To illustrate an example of implementation in Java, consider the case of an online store that offers 5% discounts to customers in either of the following cases: The current date is the first day of a month, or the total amount of the order is greater than or equal to 500 dollars. It is assumed that statistically speaking, 10% of orders have amounts totaling $500 or more. The code below is a first writing of a procedure that calculates a customer’s discount (it is assumed that customers are represented by a Customer class containing several attributes including totalOrderAmount, which indicates the total amount of the current order of the customer). This code uses the short-circuit logical operator || (OR), but the writing is not optimal.
Indeed, the first operand of this operator is the expression calendar.get(Calendar.DATE) == 1, and the probability for this condition to be true is between 1/31 (for the months of the year having 31 days) and 1/28 (in non-leap years, the month of February has 28 days), or approximately between 3.23% and 3.57%. On the other hand, according to the statistics of the online store, 10% of the orders have a total amount of 500 dollars or more, which means that the probability of the second operand of the logical operation || (the condition totalOrderAmount >= 500) being true is 10%. Thus, the second operand is about three times more likely than the first operand to be true. Therefore, it is necessary to reverse the order of appearance of these operands in the writing of the logical operator ||, as in the code below, which should make it possible to avoid a more frequent evaluation of the second operand, calendar.get(Calendar.DATE) == 1.
With this strategy, we should execute fewer operations in the end, and consequently, the energy consumption of the execution of the calculation of the discounts should be less than in the case of the first code. This example illustrates how company or business statistical data or other non-technical information could meaningfully influence how energy-efficient computer code should be written.