Tech Tip: Streamlining Option Handling with Short Circuit Evaluation
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: October 13, 2025
When initializing configuration values, it’s common to define default, optional, and override parameters.
Traditionally, developers check each object property with nested If statements to determine which value should take precedence. However, this approach can quickly become verbose and less readable.
Traditional Approach: Verbose
While correct, this pattern becomes cumbersome as the number of possible sources increases (environment variables, print settings , user preferences, etc...).
Concise Approach: Compact
4D allows the use of the || (logical OR) operator to evaluate expressions from left to right until a truthy value is found.
You can rewrite the same logic in a single line:
Traditionally, developers check each object property with nested If statements to determine which value should take precedence. However, this approach can quickly become verbose and less readable.
Traditional Approach: Verbose
// Default values var $default : Object:=New object $default.port:=54321 var $options : Object:=New object $options.port:=10000 var $overrides : Object:=New object $overrides.port:=20000 If (OB Is defined($overrides; "port")) $port:=OB Get($overrides ;"port") Else If (OB Is defined($options; "port")) $port:=OB Get($options; "port") Else $port:=OB Get($default; "port") End if End if ALERT(String($port)) |
While correct, this pattern becomes cumbersome as the number of possible sources increases (environment variables, print settings , user preferences, etc...).
Concise Approach: Compact
4D allows the use of the || (logical OR) operator to evaluate expressions from left to right until a truthy value is found.
You can rewrite the same logic in a single line:
var $port : Integer $port := $overrides.port || $options.port || $default.port ALERT(String($port)) |