Short circuit logic
04-18-2020, 08:17 AM
Post: #1
 mchris Junior Member Posts: 18 Joined: Apr 2020
Short circuit logic
I found that I can replace IFs with short circuit logic. Example:

IF a>b THEN
do_something;
do_something_else;
END;

can be replaced by:

(a>b) AND (do_something; do_something_else);

as long as do_something and do_something_else are only calls to other functions or assignments. There must not be statements like IFs, RETURNs, BEGINs, loops... But if this is a really long line the Prime finds an error in it. Any thoughts?
04-18-2020, 11:06 AM
Post: #2
 DrD Senior Member Posts: 1,132 Joined: Feb 2014
RE: Short circuit logic
My thoughts:

With tools, such as IF THEN ELSE END; and IFTE(); which are designed to accomplish this task, there is little to be gained by using the shortcut. Considering the programming effort overall, avoiding unusual syntax, (which might have unintended side effects directly, or in other releases), seems like a more dependable choice.
04-18-2020, 11:35 AM
Post: #3
 Carlos295pz Senior Member Posts: 363 Joined: Sep 2015
RE: Short circuit logic
It is for the sake of simplification, in a logical operation when using "AND", if the first value is false it will obviate the following values ​​of the same level, because in an "AND" operation, a false is enough for everything to be false.
This is a valid alternative:
Boolean_real AND Function_or_ (operation)

"Function" or "Operation" must return a real or integer number, because logical operators only accept those values.

(operation1; operation2; ...): This is a syntax error
Examples of argument error:
...

Viga C | TD | FB
04-18-2020, 11:39 AM (This post was last modified: 04-18-2020 11:54 AM by Carlos295pz.)
Post: #4
 Carlos295pz Senior Member Posts: 363 Joined: Sep 2015
RE: Short circuit logic
(04-18-2020 08:17 AM)mchris Wrote:  IF a>b THEN
do_something;
do_something_else;
END;

can be replaced by:

(a>b) AND (do_something; do_something_else);

In logical operation format it would be like this::

(a>b) AND (do_something) OR (do_something_else);

Knowing that do_something and do_something_else must return a real or integer otherwise an error occurs in execution. do_something must return a value other than 0

Example:
Code:
EXPORT LogicExpr(Boolean) BEGIN Boolean AND MSGBOX("True") OR MSGBOX("False")  END;

Viga C | TD | FB
04-18-2020, 07:52 PM
Post: #5
 CyberAngel Member Posts: 294 Joined: Jul 2018
RE: Short circuit logic
(04-18-2020 11:06 AM)DrD Wrote:  My thoughts:

With tools, such as IF THEN ELSE END; and IFTE(); which are designed to accomplish this task, there is little to be gained by using the shortcut. Considering the programming effort overall, avoiding unusual syntax, (which might have unintended side effects directly, or in other releases), seems like a more dependable choice.

Right!
IFTE is the way to go here!
– –
VPN
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)