Discussion:
Inconsistent behaviour with short boolean evaluation !!!
(too old to reply)
Skybuck Flying
2015-02-12 00:58:52 UTC
Permalink
Hello,

Delphi XE 7 behaves inconsistent concerning function result "build up":

// CASE 1 (AND): will always call all functions !
function Example1 : boolean;
begin
result := True;
result := result and FunctionA;
result := result and FunctionB;
result := result and FunctionC;
end;

// CASE 2 (OR): short boolean evaluation makes this code fail ! once
functionA returns true, the other 2 functions will not called !
function Example2 : boolean;
begin
result := False;
result := result or FunctionA;
result := result or FunctionB;
result := result or FunctionC;
end;

// This kind of inconsistent behaviour is bad !!!

// Recommendation: Always execute functions which are called on "boolean
chains !"

Bye,
Skybuck.
Skybuck Flying
2015-02-12 01:06:47 UTC
Permalink
For now I solve this problem as follows:

function Example2 : boolean;
var
vResult : array[0..12] of boolean;
vIndex : integer;
begin
result := false;

vResult[00] := Function00;
vResult[01] := Function01;
vResult[02] := Function02;
vResult[03] := Function03;
vResult[04] := Function04;

for vIndex := 0 to 4 do
begin
result := result or vResult[vIndex];

if result then break;
end;
end;

(Needless to say, this is annoying and error prone).

Hopefully my recommendation gets implemented some day ! ;) :)

Bye,
Skybuck.
Grinder
2015-02-12 13:06:11 UTC
Permalink
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsbooleanshortcircuitevaluation_xml.html
Post by Skybuck Flying
Hello,
// CASE 1 (AND): will always call all functions !
function Example1 : boolean;
begin
result := True;
result := result and FunctionA;
result := result and FunctionB;
result := result and FunctionC;
end;
// CASE 2 (OR): short boolean evaluation makes this code fail ! once
functionA returns true, the other 2 functions will not called !
function Example2 : boolean;
begin
result := False;
result := result or FunctionA;
result := result or FunctionB;
result := result or FunctionC;
end;
// This kind of inconsistent behaviour is bad !!!
// Recommendation: Always execute functions which are called on "boolean
chains !"
Bye,
Skybuck.
Skybuck Flying
2015-02-12 16:14:19 UTC
Permalink
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsbooleanshortcircuitevaluation_xml.html

I know that compiler option exists but turning off short evaluation is not
good enough, because I do want short evaluation in all other cases.

I don't want short evaluation when functions are involved. That's all... so
I hope to see improvements in Delphi XE 7.

I also consider it a bit of a bug... because those functions may actually do
something usefull... simply "disabling" something usefull just basic some
other "boolean logic" part can be short-circuited doesn't really make sense.

Bye,
Skybuck.
Skybuck Flying
2015-02-12 16:18:52 UTC
Permalink
Then again... one could argue that just the mere "act" of reading some
boolean could also trigger something.

Like an access violation.

In that case my desired behaviour would introduce a new inconsistency.

If functions were executed they could access violate, but the booleans
themselfes not.

Perhaps it's best to leave Delphi XE 7 the way it is.

I will have to live with writing around it.

Or maybe some other solution exists... perhaps indicating somehow that the
functions must be fully evaluated when on "boolean chains".

function test : boolean; no_short_circuit;
function test : boolean; full_boolean_evaluation; // or this.
begin

end;

result := result or test;

^ test always executes.

Bye,
Skybuck.
Grinder
2015-02-12 22:35:56 UTC
Permalink
Post by Grinder
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsbooleanshortcircuitevaluation_xml.html
I know that compiler option exists but turning off short evaluation is
not good enough, because I do want short evaluation in all other cases.
I don't want short evaluation when functions are involved. That's all...
That doesn't seem very consistent.
Skybuck Flying
2015-02-12 22:39:00 UTC
Permalink
Post by Grinder
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsbooleanshortcircuitevaluation_xml.html
I know that compiler option exists but turning off short evaluation is
not good enough, because I do want short evaluation in all other cases.
I don't want short evaluation when functions are involved. That's all...
"
That doesn't seem very consistent.
"

It seems to be a conflict/paradox.

On one hand it's consistent... on the other hand it's not.

Which kinda sucks.

Bye,
Skybuck.
Skybuck Flying
2015-02-12 22:41:47 UTC
Permalink
It is not as bad as it seems though.

Fortunately there is a way out of the paradox.

"Enable full boolean evaluation".

Drawback is less performance/more instructions required... nobody/most
people probably don't wanna pay that price :)

So that the funny thing.

They'd rather live with a little paradox now and then... then sacrifice
speed ;)

Bye,
Skybuck.

Loading...