How does Inform evaluate OR and AND?

If you are optimising for speed, should you think about the order of conditions in an OR or AND statement? I have been assuming that Inform evaluates a condition of the form “A or B” in this way:

  1. Check whether A is true. If so, evaluate to true. If not, go to step 2.
  2. Check whether B is true. If so, evaluate to true. Otherwise, evaluate to false.

If this is the case, you should put the statement that is most often true (or that is least costly to evaluate; or to be precise, that has the highest value for [chance to be true]/[time to evaluate]) in place A. This is faster than putting it in place B.

Is this true? Or does Inform evaluate conditions in some other way?

Inform 6 and 7 both short-circuit logical “and” and “or”, the way you’re describing.

Yes that’s how it works, so try to put them in this rough order: variable checks, property checks, simple function calls (phrases, rules, I7 adjectives), slow function calls (the ones using block values).

What if you say ‘If A is true and B is true and C is true’ - would it stop going through the checks as soon as it saw A was false?

  • Wade

That’s right, it would check only A. It’s equivalent to

if A is true: if B is true: if C is true: do something;