if mCommandList = nil or mCommandList.EOF then
Will this ever throw a nil object exception?
if mCommandList = nil or mCommandList.EOF then
Will this ever throw a nil object exception?
In my experience, no it won’t because mCommandList.EOF
is only evaluated if mCommandList = nil
evaluates to False.
It could throw an exception if the two expressions were reversed.
When a compiler uses short-circuit logic, it abandons post evaluations when it already knows the final result regardless of evaluating the entire expression. The OR keyword enables this approach, as soon as you get a true, you’re done. But the AND keyword makes the reverse, it will need to evaluate all sides to obtain the result causing this kind of side effect.
In expressive languages like PHP where all can be analyzed like expressions and without a an assignment the results discarded, it’s used as a shortcut trick in some decisions like:
fopen(“http://www.xojo.com”,“r”) or die(“Unable to connect to Xojo Site”);
This sometimes can make you commit errors like:
if importantThing1() or importantThing2() then MsgBox “Important things were evaluated and one said ok… Or not… and you have a bug”
Some compilers have a #pragma to enable/disable short circuit evaluation for parts of the code. The default is enabled.
Forgot to mention, when x() AND y(), and x is false, you’ll never call y() too. The reverse effect.
As Ricks says, you could also do this safely:
if mCommandList <> nil and not mCommandList.EOF then ...
[quote=27823:@Rick A.]When a compiler uses short-circuit logic, it abandons post evaluations when it already knows the final result regardless of evaluating the entire expression. The OR keyword enables this approach, as soon as you get a true, you’re done. But the AND keyword makes the reverse, it will need to evaluate all sides to obtain the result causing this kind of side effect.
[/quote]
And can be short circuited as well since if you get a false as one of the left most expressions there’s no way the entire expression can be true
Try it with
[quote]
Function FunctionThatReturnsTrue() as Boolean
break
return true
end function
if false and FunctionThatReturnsTrue() then
end if[/quote]
[quote=27860:@Norman Palardy]And can be short circuited as well since if you get a false as one of the left most expressions there’s no way the entire expression can be true
Try it with
Function FunctionThatReturnsTrue() as Boolean
break
return true
end function
if false and FunctionThatReturnsTrue() then
end if
[/quote]
You probably skipped my ( if false AND anything() ) comment above: https://forum.xojo.com/conversation/post/27825