I had a question as to the current thinking on the use of global variables. Just to demonstrate, I might have a script which is organized as follows:
main()
set aVariable to "a value"
-- other stuff
handlerOne(aVariable)
handlerTwo(aVariable)
-- 3 or more additional handlers all of which need aVariable
end main
on handlerOne(aVariable)
end handlerOne
on handlerTwo(aVariable)
end handlerTwo
-- 3 or more additional handlers all of which need aVariable
main()
The alternative to the above might be:
global aVariable
main()
set aVariable to "a value"
-- other stuff
handlerOne()
handlerTwo()
-- 3 or more additional handlers all of which need aVariable
end main
on handlerOne()
end handlerOne
on handlerTwo()
end handlerTwo
-- 3 or more additional handers all of which need aVariable
main()
Apparently, the second alternative may cause aVariable to be saved with the script every time it’s run, which seems undesirable. My questions:
-
How do other forum members handle this matter.
-
Is there some way to stop the script from saving aVariable if set as global with the script every time it’s run?
Thanks!