Skip to content

Matt Neuburg’s Advice on Declaring Variables

As I fiddle with AppleScript Studio, I have been reading AppleScript: The Definitive Guide, 2nd Edition by Matt Neuburg.

Here is a piece of advice he offers after he explains the complexities of scope for properties, global, and local variables in chapter 10:

The ideal way to manage scope, in my view, would be to declare everything. If you need a variable to be visible in this scope and deeper scopes, then declare a property. (In a handler, merely declaring a local will do.) Otherwise, declare a local. Use global declarations sparingly, only for special occasions when separate regions of scope need shared access to a value. If you follow these rules, then any undeclared variable must be a free variable (see the next section). Thus you always know what everything is. Unfortunately, as I’ve said, AppleScript doesn’t help, so you can’t easily and reliably follow these rules, even if you want to. (( Quote from section 10.7 ))