Short-circuit evaluation
From Wikipedia, the free encyclopedia
Programming evaluation |
---|
Short-circuit evaluation or minimal evaluation denotes the semantics of some boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and
evaluates to false
, the overall value must be false
; and when the first argument of or
evaluates to true
, the overall value must be true
. In some programming languages (Lisp), the usual boolean operators are short-circuit. In others (C, Ada), both short-circuit and standard boolean operators are available.
The short-circuit operator x Sand y
is equivalent to the conditional expression if x then y else false
; x Sor y
is equivalent to if x then true else y
.
Short-circuit operators are, in effect, control structures rather than simple arithmetic operators, as they are not strict.
In loosely-typed languages which have more than the two truth-values True
and False
, short-circuit operators may return the last evaluated subexpression, so that x Sand y
is actually equivalent to if x then x else y
(without actually evaluating x
twice). This is called "Last value" in the table below.
Language | Standard operators | Short-circuit operators | Value |
---|---|---|---|
C , Java | & , | |
&& , || |
Boolean |
Javascript | & , | |
&& , || |
Last value |
Lisp | none | and , or |
Last value |
Ada | and , or |
and then , or else |
Boolean |
Perl | & , | |
&& , and , || , or |
Last value |
Python | & , | |
and , or |
Last value |
[edit] Example
Consider the following example using the C programming language:
int a = 0;
if (a && myfunc(b)) {
do_something();
}
In this example, short-circuit evaluation guarantees that myfunc(b)
is never called. This is because a
evaluates to false. This feature permits two useful programming constructs. Firstly, if the first sub-expression checks whether an expensive computation is needed and the check evaluates to false, one can eliminate expensive computation in the second argument. Secondly, it permits a construct where the first expression guarantees a condition without which the second expression may cause a runtime error, such as in the following C code where minimal evaluation prevents a null pointer dereference:
int is_three_chars_long(const char *p) {
return (p != NULL) && (strlen(p) == 3);
}
Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code
if (expressionA && myfunc(b)) {
do_something();
}
if myfunc(b) is supposed to perform some required operation regardless of whether do_something() is executed, such as allocating system resources, and expressionA evaluates as false, then myfunc(b) will not execute, which could cause problems. Some programming languages, such as Java, have two operators, one that employs minimal evaluation and one that does not, to avoid this problem.
It is worth noting that these expressions may be considered a more compact way of expressing a nested branch or sequence of branches. For example, consider the following block of code, when minimal evaluation is in use:
if (CONDITION_A && CONDITION_B) {
TRUE_BRANCH
} else {
FALSE_BRANCH
}
becomes (through logical AND expansion)
if (CONDITION_A) {
if (CONDITION_B) {
BOTH_TRUE
} else {
B_FALSE
}
} else {
A_FALSE
}
Note that, due to the nature of the boolean logic involved, both the A_FALSE and B_FALSE code branches are represented by the single FALSE branch in the first example. However, following the more verbose second example does, at least, have one possible advantage — different code can be executed depending upon which condition, if any, is FALSE. Note however that if both conditions are false, only A_FALSE will be executed, and not B_FALSE.
A compiler using the common subexpression elimination and constant propagation optimizations might generate the same code for all such versions.
Minimal evaluation can also be used to conditionally execute some code for side effects, as in the common Perl idioms:
some_condition or die; # Abort execution if some_condition is false
some_condition and die; # Abort execution if some_condition is true