Do you dream of using short-circuit evaluation, ternary operators, breaking free from a loop, or exiting from a method with a simple keyword?
Well, dream no more! We listened and kept an eye on your feature requests from the 4D Forum, so here comes a set of 4D language improvements that will help you simplify your code.
return, continue, and break
We add 2 new statements that allow you to jump out from a block of code:
- The return keyword terminates the execution of the method or function in which it appears and returns control to the calling method. It can also return an optional value:
function factorial ( $n : Integer) : Integer
If ($n<1)
return 1
Else
return $n*factorial($n-1)
End if
- The break keyword terminates the loop containing it. The following statement executed is the one immediately after the body of the loop:
For ($i; 1; 100)
If ($myTab{$i}="")
break
End if
End for
If ($i<101)
// your code
End if
- The continue statement terminates execution of the statements in the current iteration of the current loop and continues execution of the loop with the next iteration:
For ($i; 0; 10)
If ($i=3)
continue
End if
$text+=String($i)
End for
// $text="012456789"
short-circuit evaluation
In an expression that uses an AND or OR operator, a short-circuit evaluation means that the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression. We’ve added two new operators to do it:
This new way to write your AND and OR operators allows you to write this type of test in one line:
if (($myColl.length>0) && ($myColl[$i]>10))
// your code
End if
If $myColl is empty, the second argument is not executed, so no error is thrown.
So, of course, all of you understand this writing style’s interest easily to make your code more readable. But it allows you to go further.
When you write expr1 && expr2 or expr1 || expr2, 4D evaluate both expressions truthy or falsy nature. The values listed below are considered as falsy:
- false
- Null
- undefined
- Null object
- Null collection
- Null pointer
- Null picture
- Null date
- “” – Empty strings
- [] – Empty collections
- {} – Empty objects
All other values are considered truthy.
The || operator returns the first expression if the first expression is truthy, otherwise it returns the second.
The && operator returns the first expression if the first expression is falsy, otherwise it returns the second.
For example, the code below doesn’t return a boolean but a numeric value:
$salary:= $employee.salary || $minSalary
$employee.salary is evaluated first. If its result is not Null, it is considered as truthy and returned.
If $employee.salary is Null, and thus falsy, the second expression ($minSalary) is evaluated and its result is returned.
compound assignment Operators
Compound assignment operators are a simplified form of writing operations:
- The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable.
$a+=5
// equals to $a:=$a+5
- The subtraction assignment operator (-=) subtracts the value of the right operand from a variable and assigns the result to the variable.
$s-=5
// equals to $s:=$s-5
- The division assignment operator (/=) divides a variable by the value of the right operand and assigns the result to the variable.
$d/=2
// equals to $d:=$d/2
- The multiplication assignment operator (*=) multiplies a variable by the value of the right operand and assigns the result to the variable.
$m*=5
// equals to $m:=$m*5
Ternary operator
A ternary operator is a condition followed by a question mark ( ? ), an expression to evaluate if the condition is truthy followed by a colon ( : ). Finally, the expression to evaluate if the condition is falsy: condition ? value If True : value If False
For example, you can write:
$label:=($size>1000) ? "big" : "small"
The ternary operator is very similar to the Choose command except that Choose accepts only boolean or integer expression as a condition when the ternary operator accepts all types and tests their falsy and truthy status.
More information is available here.