Best practices for Arcade expressions

Some applications of ArcGIS Arcade are better suited than others for specific use cases. The following are some of the recommended best practices for using Arcade expressions in ArcGIS Velocity analytic tools:

  • Expression complexity—While Arcade allows you to write complex scripts, declare variables, and define and call functions, when building Arcade scripts in Velocity it is recommended that you keep them as simple as possible. Complex scripts tend to have more potential for errors, but more importantly, they also have more impact on analytic performance. Shorter scripts and expressions are interpreted more quickly and have a lower impact on processing speed.
  • Expression length—Arcade supports lengthy multiline scripts, but just as with complex scripts, long scripts may also adversely impact processing speed. It is recommended that you keep scripts as short as possible to avoid unnecessary decreases in performance.
  • Avoid loops—When writing scripts and programs, it can be useful to leverage looping routines that iterate a set of data and repeat a procedure or test for specific conditions. Although this is supported in Velocity, it can reduce performance of analytics, sometimes severely. Use looping logic infrequently and judiciously in analytic expressions.

    Avoid conditions in your expressions that may lead to infinite looping when the analytic is running. If this occurs, you may observe a significant impact to the analytic's performance.

    When Velocity validates an analytic and determines that it may contain looping logic, a validation warning is returned. This does not prevent you from running the analytic, but it serves as a reminder that loops can have a negative impact on performance.

  • Testing an expression with sample values—Testing with sample values may be necessary to produce the correct data type from the configured inputs.

    For example, if you have a string field named BaseDateTime containing date values in ISO 8601 format, such as 2019-04-05T12:05:18.095Z, and you want to parse the strings into dates, you can use an expression such as Date($feature.BaseDateTime). The Arcade expression builder attempts to validate the expression using a sample string. The default sample string, however, is Pacific, which does not successfully parse to a date. Once the expression builder is closed, a validation error is returned indicating that you must configure a valid expression that returns data in the target field type.

    editor testing expression with default value

    You can bypass this behavior by passing in your own valid sample string in the expression builder. To do so, reopen the expression builder, and on the Globals tab, click the edit button next to the string field containing the date information.

    editor editing the sample value

    Copy an example string in the same date format as the values in the field. When you next test the expression, a valid date value will be generated, and the expression can be used in the tool successfully.

    editor testing expression with sample value