Hi!
I seem to remember seeing a thread once upon a time, which suggested that the use of global variables was not such a good idea. Am I imagining things?
I’m trying to weigh up the pros and cons of using a number of globals in a script. Here’s a snippet of what I use at present…
drawObjects(pgW, pgH, horObj, verObj, blVal, guVal, blHd) – do stuff end
This parameter list is likely to increase as I extend the functionality of this particular script. Is this good practice or should I use globals instead to save passing all these variables? Or maybe there’s another way…
ANY information would be useful.
Thanks,
Juerg
There is no reason in applescript not to use globals or properties. There is no reason in a sufficiently short script not to use a global. Globals are bad for one very good reason: in a long program where a global variable is passed to several different subroutines, each of which modifies the global, the programmer can become confused about the contents of the global. The program will not then work as expected.
: I have used globals extensively in many of my scripts without any
: problems. I just learned how to use script objects to set up
: variables with the same set of properties (thanks to this
: group). See my thread “Create a Class” (or something
: like that) posted earlier this week.
: Hope this helps, Brad Bumgarner
Thanks Brad!
I’ve seen script objects before but have never looked into them properly. I think I might restructure some existing scripts to make use of script objects and libraries.
Also thanks to Mac Guy!
Thanks for the pointer,
Juerg
: There is no reason in applescript not to use globals or
: properties. There is no reason in a sufficiently short script
: not to use a global. Globals are bad for one very good reason:
: in a long program where a global variable is passed to several
: different subroutines, each of which modifies the global, the
: programmer can become confused about the contents of the
: global. The program will not then work as expected.
Good point! When my programs get long, I create a second file listing all of my global variables and their purpose. I also list all of my subroutines and their purpose. Sometimes that is the ONLY way I can keep track of everything!
Brad Bumgarner