Unpopularity of global variables - why?

Graham Samuel livfoss at mac.com
Fri Oct 12 16:50:30 EDT 2012


The discussion about Strict Compile Mode brought in a lot of stuff about globals, and I sense that many people think they're a bad thing - I am not talking about trick ways of using them, just regular globals that allow one to refer quantities (numbers, strings, anything really) across scripts which are housed in different objects in the same program (set of stacks).

The thing is, I can't see the objection. Clearly any technique can become sufficiently messy and obscure as to negate its own usefulness, but what's wrong with the idea itself? If I want to maintain for example a status across a whole program, if I don't use a global, I am going to have to use a custom prop or a function to transmit that status across object boundaries, and that means more typing with no more security, so why do it? What's wrong with

   if gMy_Status is "open"...

compared to

   if fMy_Status() is "open"…

or

   get the cpMy_Status of stack "myStack"
   if it is "open"…

Again, since the 'constant' command doesn't have global status, what's wrong with using globals to store program-wide constants? Is any other way cleaner?

Obviously I'm missing the point - I know I am. It's probably my age. But I'd like to see a more reasoned attack on globals before I give them up.

Graham



More information about the Use-livecode mailing list