RE: Penalty for using globals?
- Posted by Sabal.Mike at notations.com Jun 17, 2002
- 444 views
One of the things I found when writing programs in Pascal (before I had found Euphoria) was that most of my programs contained one or two master arrays that needed to be accessed or adjusted by virtually every routine in the program. For those master arrays (i.e., the checkerboard in a checkers game), it made more sense to declare them as global and comment each routine that was modifying the array globally. Especially for large arrays, the benefits included faster code and better memory use (not needing to copy the whole array twice just to change one element and less typing. In Euphoria, the penalty for passing a large sequence to a routine is zero, until the routine modifies the sequence. For that reason, I continue to use global variables for master sequences and routine flags (such as those that indicate when a dialog is open in a Windows program). Depending on the size of the program, all other variables should either be local to the file or local to each routine. File pointers in particular are best passed to each routine <disclaimer: good advice applies equally to the giver as to the recipient <G>>. Overuse of global variables is as much an enigma to good programming as the overuse of goto. As a general rule, only globalize those variables that are large and will be modified by many different routines, or those variables that need to be seen across several files. As much as possible, strive to use as few globals as possible. Oh, and don't forget to document liberally. Michael J. Sabal >>> jorfergie03 at yahoo.com 06/17/02 10:21AM >>> SR.Williamson wrote: > What kind of penalty can I expect from using a lot of global variables > or sequences? Speed? Memory use? > > > There must be some speed/memory loss but both must be negligable. If writting big apps that have lots of include files then the only problem you'll face is confusion or naming conflicts. jordah