Unpopularity of global variables - why?

Mark Wieder mwieder at ahsoftware.net
Fri Oct 12 17:01:51 EDT 2012


Graham-

Friday, October 12, 2012, 1:50:30 PM, you wrote:

> Obviously I'm missing the point - I know I am. It's probably my
> age.

:-P

> But I'd like to see a more reasoned attack on globals before I
> give them up.

Generally speaking (there are exceptions) the main reason global
variables are a bad idea is that they have no protection. Any script
in any stack can change the value of a global variable behind your
back, and then it's a real pain trying to figure out what's going on.

If you declare and use a global variable "myGlobal" in one stack, then
close that stack and create a new one, that variable is still in
memory. If you create a new script and say "global myGlobal" you've
automatically inherited the existing value without having put anything
into it.

The situation gets worse if you have both stacks in memory at the same
time and then they're fighting over the value of myGlobal. And worse
yet if you accidentally declare a global variable that's already in
use by one of the system stacks or plugins.

-- 
-Mark Wieder
 mwieder at ahsoftware.net





More information about the Use-livecode mailing list