Tech Tip: Checking for parameters passed to a method
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: August 23, 2017
When writing a method that accepts parameters, it is important to check for the presence of the variable before attempting to access it. The following code snippet is an example method that is not checking for parameters:
C_LONGINT($0;$1;$2) $0:=$1+$2 |
The code snippet above simply returns the sum of 2 numbers. This code does compile but it is not safe because it does not check the number of parameters before using the parameters.
Here is the improved code:
C_LONGINT($0;$1;$2) If (Count parameters=2) $0:=$1+$2 End if |
The snippet above adds a conditional check to make sure the parameters exist before attempting to work with the parameters. This is a much safer way to write the method.