The GW Micro blog has been discontinued. For instant updates on GW Micro products and events, follow us on Twitter, and like us on Facebook.
How I Learned to Stop Worrying and Love the Global Variable
by Aaron Smith on Wednesday, January 7 2009Global variables are not bad. There. I said it. I hear the moaning, the groaning, the pounding of fists, and feel the wagging of fingers. "Blasphemy," they scream -- pitchforks and torches bobbing in the air. Truth is, global variables are a necessary part of scripting, especially when using VBScript.
Storing values of registered hotkeys, handles to events, and information that needs to be accessible to all functions and subroutines (i.e. external strings, file locations, etc.) must take place in the global scope.
If you examine the reasons why people decry the use of global variables, they pretty much boil down to one thing: maintainability. In complex code, managing global variables can be a daunting task. Trying to remember a variable's scope when you're deep in the trenches of some random function can be challenging. So here are a couple of tips to help overcome global variable-itis.
If you're having a difficult time remembering what variables go with what functions, put the function name in the variable name. For example, say you have two collections of registered hotkeys, and two functions that manipulate those keys (KeyFunctionsHot, and KeyFunctionsCold). Rather than using variable names like mykeys1, and mykeys2, put the function names in the variables names: keysForKeyFunctionsHot, keysForKeyFunctionsCold.
What do you think the purpose is of the following variables: a, VarX, myObj. If you said, "I have no freakin' idea," you'd be right. Variable names are much more useful when they describe the meaning of their existence, such as: countA, clipPositionX, and myActiveWindow. While you still need more context to understand the purpose of those variables, their names lend a clue about what they're used for.
Indicating a variable's type in the variable name not only reveals clues about their use, but also saves time in having to look up what the type should be. If you have a variable that contains a string, indicate that in the variable name, such as myNameString, strMyName, or sMyName. If you have a variable that contains an object, indicate that in the variable name, such as myWindowObject, objMyWindow, or oMyWindow.
When creating dialogs, put the type of the control in the id attribute for easy reference, such as edit_name, or btn_close. Ok, so that has nothing to do with variables, but you get the idea that proper naming helps understanding code, both for you, and anyone else trying to figure out what you're up to.
These kinds of naming conventions can save a lot of time and headaches when dealing with global variables, even variables in general.
If you just can't bring yourself to accept that global variables are good, that's fine. To each their own. Remember that Window-Eyes scripting supports all ActiveScript languages, in addition to any other language that supports COM automation (such as C++, and Visual Basic). You have the power of choice, so use a language that best suits you.




