Arcade expressions may be authored in any text editor. ArcGIS Pro and ArcGIS Online have expression editors that provide help for debugging expressions. You can also use the playground for testing Arcade expressions.
Playground
The playground provides an integrated text editor for writing Arcade expressions, with tools such as code completion, syntax highlighting, suggestions, error checking, and more. To learn more about the Arcade editor, check out the following blog post: Discover the powerful new features of the Arcade Editor.
Console
The Console function allows you to log messages and values to the console of the playground or expression editor in ArcGIS Online or ArcGIS Pro. When writing expressions in web apps using the ArcGIS Maps SDK for JavaScript, this function also logs messages in the browser's console window.
var i = 0;
i++;
Console(i); // logs `1` in the console
return ++i; // returns `2` from the expression
This function is useful when debugging expressions that define complex functions or iterate through items in a loop. It can also be useful for checking the values of profile variables.
var total = 0;
for(var att in $feature){
total += feature[att];
console("attribute:", att);
console("value:", feature[att]);
console("total:", total);
}
return total;
// Console will log the following for each iteration in the loop:
// attribute: "Population"
// value: 1000
// total: 50000
This can help you identify where a loop or a statement in a function fails.
Comments
When a expression throws an error, you can use comments to disable statements line-by-line to understand where the expression behaves unexpectedly. Because this can be time consuming, it is often used as a last-resort technique.
var dogs = $feature["dogs"];
var cats = $feature["cats"];
var rats = $feature["rats"];
var turtles = $feature["turtles"];
var birds = $feature["birds"];
var groups = [
dogs,
cats,
rats,
turtles,
birds,
];
function simpsonsDiversityIndex(vals){
var k = Array(Count(vals));
var t = sum(vals);
return t;
// disable the code below to see if the sum of
// the array's values is returning an unexpected result
// for(var i in vals){
// var n = vals[i];
// k[i] = n * (n-1);
// }
// var s = Sum(k);
// var di = 1 - ( s / ( t * (t-1) ) );
// return Round(di*100);
}
simpsonsDiversityIndex(groups);
Replace profile variables
It's a good idea to test your expression with various input values, such as large numbers, small numbers, negative numbers, all zeros, all nulls, etc. This helps you write a robust expression.
Assigning profile variables to your own descriptive variable names helps you not only write a more readable expression, but it also helps when testing various combinations of values. The following example shows an abbreviated expression to conserve space.
// the field names here aren't descriptive. Assigning them
// to descriptive field names makes it easier to write the expression
var dogs = $feature["ANCOD_001"];
var cats = $feature["ANCOD_002"];
var rats = $feature["ANCOD_003"];
var turtles = $feature["ANCOD_004"];
var birds = $feature["ANCOD_005"];
// expression continues and uses the variables
// ...
To test various scenarios you can do something like this:
var testValues = [100,132,1234,0,123]; // returns 68.3 GOOD!
// var testValues = [100,0,0,0,0]; // returns 0 GOOD!
// var testValues = [0,0,0,0,0]; // returns 0 GOOD!
// var testValues = [100,132,1234,0,null]; // throws error - FIX THIS!
// var testValues = [null,null,null,null,null]; // throws error - FIX THIS!
var dogs = testValues[0] // $feature["ANCOD_001"];
var cats = testValues[1] // $feature["ANCOD_002"];
var rats = testValues[2] // $feature["ANCOD_003"];
var turtles = testValues[3] // $feature["ANCOD_004"];
var birds = testValues[4] // $feature["ANCOD_005"];
// expression continues and uses the variables
// ...
If you get the expected results for one set of test values, simply disable that line and enable the next line of test values, annotating the results as you test.
// var testValues = [100,132,1234,0,123]; // returns 68.3 GOOD!
var testValues = [100,0,0,0,0]; // returns 0 GOOD!
// var testValues = [0,0,0,0,0];
// var testValues = [100,132,1234,0,null];
// var testValues = [null,null,null,null,null];
var dogs = testValues[0] // $feature["ANCOD_001"];
var cats = testValues[1] // $feature["ANCOD_002"];
var rats = testValues[2] // $feature["ANCOD_003"];
var turtles = testValues[3] // $feature["ANCOD_004"];
var birds = testValues[4] // $feature["ANCOD_005"];
// expression continues and uses the variables
// ...
Continue this process for each line of test values. By the end of your tests, you will have results that look something like this:
// var testValues = [100,132,1234,0,123]; // returns 68.3 GOOD!
// var testValues = [100,0,0,0,0]; // returns 0 GOOD!
// var testValues = [0,0,0,0,0]; // returns 0 GOOD!
// var testValues = [100,132,1234,0,null]; // throws error - FIX THIS!
var testValues = [null,null,null,null,null]; // throws error - FIX THIS!
var dogs = testValues[0] // $feature["ANCOD_001"];
var cats = testValues[1] // $feature["ANCOD_002"];
var rats = testValues[2] // $feature["ANCOD_003"];
var turtles = testValues[3] // $feature["ANCOD_004"];
var birds = testValues[4] // $feature["ANCOD_005"];
// expression continues and uses the variables
// ...
After fixing the broken scenarios, simply remove the comments and replace the test values with the profile variables.
var dogs = $feature["ANCOD_001"];
var cats = $feature["ANCOD_002"];
var rats = $feature["ANCOD_003"];
var turtles = $feature["ANCOD_004"];
var birds = $feature["ANCOD_005"];
// expression continues and uses the variables
// ...