Many developers may utilize the concept of Booleans within their applications for various reasons. Every value within 4D has a Boolean value it is associated with that developers can take advantage of for use with short-circuit or ternary operators. Short-circuit operators are useful because they do not necessarily need to evaluate every operand. Values that are considered falsy are false itself, any Null value, undefined, and empty values. Everything else is truthy, including the numeric value 0, not to be confused with a null date !00-00-00! which is falsy.
The way that 4D determines a value should be true or false is its usability, which is why the numeric value 0 is considered truthy for these operators; 0 is a usable value. The following shows the truthiness of 0 in short-circuit operators.
//Returns 0 (expression 1) because if expression 1 is true it does not need to evaluate expression 2 $num:=0 || 1 //Returns 2(expression 2) because it will evaluate until it reaches either a false value or the last value. This case it is the last value. $num:=0 && 2 //Returns 0(expression 2) because: false Exp1 || true Exp2 = Exp2 $num:=Null || 0 |
The below example shows a use case for these operators where either the email will be stored or if there is none, it will be marked "n/a". This is shorter than writing an if statement to check for an undefined or empty value.
var $email : Text //$mail is undefined, $email will be set to n/a $email:=$mail || "n/a" $mail:="name@mail.com" //$mail is now defined so $email can be set to that value $email:=$mail || "n/a" |