Short-circuit evaluation

Link to this posting

Postby Ursego » 21 Feb 2013, 22:27

Use short-circuit evaluation to improve performance of your program.

Short-circuit evaluation (SCE), also called "minimal", denotes the semantics of Boolean operators in logical expressions in formats "A AND B" and "A OR B" in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression. In other words, the evaluation of a Boolean expression terminates as soon as the result value, produced by the whole expression, is known, even if not all the components in the expression have been evaluated. Thus, try to build the expression so that the decision is made as soon as possible:

"A AND B" ("A && B"): when A evaluates to false, the whole expression will return false (B will not be evaluated), so make sure that A is more efficient (executed quicker) than B (and if they have a same performance, make sure that A is more likely to produce false than B).

"A OR B" ("A || B"): when A evaluates to true, the whole expression will return true (B will not be evaluated), so make sure that A is more efficient (executed quicker) than B (and if they have a same performance, make sure that A is more likely to produce true than B).

To clarify the idea, let's write a Boolean method which determines if the person is eligible to work in security (to pass the validation, the person must be older than the minimum age, defined by law, AND to have no criminal record). The age checking is simply a quick comparison of instance variables, but to check criminal record, the application needs to call a web service (which is much slower operation). Here is the method's code with the correct order of components (PB doesn't have short-circuit evaluation, so we have to imitate it by slightly more complicated code):

Code: Select all
if this.ii_age < this.ii_min_age then return false
return this.uf_has_clear_criminal_record() // will not be called if the previous condition is true

Code: Select all
return (this.age >= this.minAge && this.hasClearCriminalRecord());

So, if the candidate is immature, the web service will not be called at all.

The short-circuit is not the only possible evaluation, there is also the regular one (also called "eager" or "long-circuit") - when the second part of the Boolean expression is evaluated anyway, even if its result cannot change the already known value, which will be produced by the whole expression. In most programming languages you can utilize both the evaluations using different logical operators, but some languages have only one. Look at this table to clear up a question what happens in your programming language. If it doesn't appear in the table then you can easily check if it supports SCE by assigning a Boolean variable the results of the expressions, having zero divide in the second part (translate them to your language if needed):

Code: Select all
booleanVariable = ((1 = 2) AND (1 = (1 / 0)))
booleanVariable = ((1 = 1) OR (1 = (1 / 0)))

If you don't get the zero divide error then SCE is supported.
User avatar
Site Admin
Posts: 141
Joined: 19 Feb 2013, 20:33

Link to this posting

Postby zoeyku » 19 Nov 2013, 01:35

So strange that no PB book provides this pretty basic information...
Posts: 2
Joined: 19 Nov 2013, 01:32
Location: usa

IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:

Traffic Counter

free counters

eXTReMe Tracker