Avoiding Global Variables - revUp 93

Andre Garzia andre at andregarzia.com
Mon Jun 7 12:51:17 CDT 2010


Here is an excerpt of Andre Alves Garzia Global Guidelines:

Don't use them, they pollute the environment and can cause collisions.
Explanation: First, anyone can write to a global variable, any stack running
at the same engine can write there, so if you do a lot of work inside the
IDE for example, using globals is not safe, by not safe I don't mean it will
break things but that your global might be overwritten by some other stack
and you will not know it or be able to control it.

There's hardly a case where you can't replace it with a script local
variable. Script local variables are much like globals except for the fact
that they are not. They are only shared at script level and that is most of
the cases where you need variable sharing anyway. If you want to share data
across different scripts, then your best option is using functions because
they enable abstraction and encapsulation of your code and are safer than
globals since the underlining implementation of the data you're storing is
abstracted away which makes reusing or refactoring simpler.

In the cases where you have absolute control over the environment, then, you
can use globals as a simple way of sharing data structures. Usually for
these cases it is better to create a single global as an array and write to
a single variable, this makes debugging easier. Better to have a single
array with 50 entries than 50 globals.

For example, one place where globals are king is RevServer another place
where globals should not be used is on Rev IDE Plugins.

On Sun, Jun 6, 2010 at 1:52 PM, Richard Gaskin
<ambassador at fourthworld.com>wrote:

> Robert Earp wrote:
>
>  I'm concerned that readers of the article, especially new people to
>> Rev and scripting, will now stay away from global variables, which
>> would be a shame as they are extremely valuable.   I believe the crux
>> of the Olivers concerns about global variables is one of scripting
>> practices and project documentation, rather than global variables
>> themselves.
>>
>> There's absolutely nothing wrong with global variables, in fact I much
>> prefer them to other means of storing data especially when wishing to
>> access that data across multiple stacks and/or locations.  Global
>> variables are truly global, and as such save an awful lot of
>> addressing problems which ends up in simpler script.  But I do agree
>> in using custom properties of objects, or groups, when the data truly
>> refers to something associated with the object/group.
>>
>
> Thank you for that, Bob.
>
> There is indeed a stigma against global variables, and while a good many
> programmers continue to argue against their use nearly every language they
> use supports them.  I doubt the designers of programming languages are all
> so wrong.
>
> Like you noted, there's a place for globals, and it's also true that
> globals can be abused, just as custom props and other containers can be
> abused.
>
> Rather than lead newcomers down the dogma road of avoiding globals
> altogether, it would be more helpful to assist them in distinguishing when
> they are useful.
>
> --
>  Richard Gaskin
>  Fourth World
>  Rev training and consulting: http://www.fourthworld.com
>  Webzine for Rev developers: http://www.revjournal.com
>  revJournal blog: http://revjournal.com/blog.irv
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>



-- 
http://www.andregarzia.com All We Do Is Code.



More information about the use-livecode mailing list