Avoiding Global Variables - revUp 93

Jeff Massung massung at gmail.com
Sun Jun 6 12:05:33 CDT 2010


Global variables are far from bad.

Ages ago, back in the 8-bit and 16-bit CPU days, global variables were
cursed because they required crossing segment boundaries (64K each), which
causes numerous performance hitches. No such problem exists today. In fact,
global variables can actually be a performance boost when accessed properly
because if you use them in lock-step they may all end up being in L1/2
cache.

Then academia became involved and global variables were stigmatized due to a
thought that they lead to poor programming practices. Yes, there are those
who use them inappropriately (read: for everything). But without global
variables we wouldn't have dynamic extent or scope, which are incredibly
powerful tools at our disposal as programmers.

The rule of thumb is simple: don't let data extend outside the scope in
which it is intended. If you have a loop variable, it's local to the
function that contains it. If you have a function, it's extent is likely the
module it's contained in (in Rev's case, keep functions that act on multiple
controls at the card level... and functions that act on cards at the stack
level, etc).

As Richard noted: dogma is a bad thing. Understand the problem, understand
the solution. Don't complicate the solution... instead, try and simplify the
problem. Use the tool that best helps you to get the job done. Make it work,
then go back and make it right. Then go back again and make it fast.

Jeff M.



More information about the use-livecode mailing list