Boolean expressions are evaluated sequentially

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Boolean expressions are evaluated sequentially

Post by dunbarx » Fri Dec 11, 2020 6:42 pm

Has this been discussed before?

I am building a CAD-like drawing program in LC, and as a convenience I made a gadget to allow me to hide graphics if I simply click on them with the optionKey. I want only graphics to hide. So I tested three conditions:

Code: Select all

  if the optionKey is down and the mouseControl <> empty and the name of the mouseControl contains "graphic" 
   then hide the mouseControl  --works fine
   
   --if the optionKey is down and the name of the mouseControl contains "graphic" and the mouseControl <> empty
   --then hide the mouseControl  --fails
In version two the condition "and the mouseControl <> empty" comes third, after the other two, as opposed to second, between the other two, in version one. That version fails with "no target found" if I click on empty card space,

The card object is not a mouseControl, the function returning empty.

I assume this means that each condition is evaluated sequentially, and clicking on empty space throws an error since the test for graphic "fails" before the empty result from the function "fails".

The pecking order matters, even though each condition on its own is identical between the two versions. I do not see a reason for this at all.

Expected behavior?

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10054
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Boolean expressions are evaluated sequentially

Post by FourthWorld » Fri Dec 11, 2020 7:26 pm

Yes, like Python and others LC uses "lazy" evaluation of conditions, where it only evaluates as much as is necessary to determine branching.

One benefit of this is performance; it simply saves time evaluating things that don't make a difference in execution flow.

Another benefit is what you've found in your example: it allows you to order your conditions in ways that might otherwise throw errors even where a later condition is inconsequential.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Boolean expressions are evaluated sequentially

Post by SparkOut » Fri Dec 11, 2020 7:34 pm

I think this is expected, and desired, as a result of lazy evaluation.
With boolean AND tests, if condition 1 fails to return true then the whole test is going to fail, so there's no point in even bothering to check the other conditions.
It follows that where maximum efficiency is desired, conditions should be evaluated in order of most generic to most specific likelihood to fail. But chances are this is unnoticeable in most situations.

[Edit, near simulposting, in agreement with Richard]

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: Boolean expressions are evaluated sequentially

Post by dunbarx » Fri Dec 11, 2020 8:57 pm

Lazy evaluation is one thing, and perfect for someone like me. I figured as much, and mentioned that I recall this being discussed a while ago.

But my main point was not that the logic of my string of booleans was not working the way I intended, but that LC threw an error and halted the handler. I get that my gadget has to be carefully constructed. I do not understand the error.

Craig

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Boolean expressions are evaluated sequentially

Post by SparkOut » Fri Dec 11, 2020 10:31 pm

Ah, so the problem is that an error is thrown by a statement that reduces to

Code: Select all

put the name of ""
when the mouseControl is empty, it has no reference to an object from which to extract the name?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10054
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Boolean expressions are evaluated sequentially

Post by FourthWorld » Fri Dec 11, 2020 10:41 pm

dunbarx wrote:
Fri Dec 11, 2020 8:57 pm
...my main point was not that the logic of my string of booleans was not working the way I intended, but that LC threw an error and halted the handler. I get that my gadget has to be carefully constructed. I do not understand the error.
Sparkout got it.

Breaking it down to psuedocode:

Works:
- Test 1: Does the control exist?
- Test 2: What is the name of the control?

Throws error:
Test 1: What is the name of the control?
Test 2: Does the control exist?

Something that doesn't exist can have no name (unless it's a cowboy gunslinger <g>).
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: Boolean expressions are evaluated sequentially

Post by dunbarx » Fri Dec 11, 2020 10:58 pm

Aha,

Thanks. That makes sense.

Craig

Post Reply