Stack Global Variables - How Bad are they? - Solved

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Stack Global Variables - How Bad are they? - Solved

Post by DR White » Sun Nov 29, 2015 4:09 pm

I have heard that one should not use Stack Global Variables, it is an easy way to use the variable anywhere in the stack.

So, I am using LC 7.1 and I am using about 100 Stack Global Variables. but is it that likely to affect the performance on a game program that is already slowing down the Android Processor?

Thanks,

David
Last edited by DR White on Sun Nov 29, 2015 4:46 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Stack Global Variables - How Bad are they?

Post by dunbarx » Sun Nov 29, 2015 4:30 pm

Hi.

There is nothing wrong with global variables. They must be declared in each script where they are used, and above any handler that uses them.They cost nothing.

There is a general feeling that custom properties are more useful, and have the advantage of surviving sessions. They need not be declared at all. I agree with this. But that is a matter of style. They are not "visible" in a script like globals are. This is a slight disadvantage.

Do you really need 100? Why so many? I bet a better methodology, perhaps at the basis of your project, would obviate the need for them, or at least allow a single artifact to substitute.

Craig Newman
Last edited by dunbarx on Mon Nov 30, 2015 6:42 pm, edited 3 times in total.

DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Stack Global Variables - How Bad are they?

Post by DR White » Sun Nov 29, 2015 4:46 pm

Craig,

I am sure that you are right. If I worked on it, I could probably get by with 20 - 30% less. I will need to continue to improve my methodology.
I do not know much about custom properties.

Thanks for the insight that global variables cost nothing,

David

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Stack Global Variables - How Bad are they? - Solved

Post by Klaus » Sun Nov 29, 2015 4:53 pm

Hi David,

100 global variables? :shock:
I would use an ARRAY instead (global or in a custom property), easier to fill, manage and access :D


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Stack Global Variables - How Bad are they? - Solved

Post by dunbarx » Sun Nov 29, 2015 5:05 pm

Chiming in again, since Klaus and I are on the same tack.

Managing tens of globals is tiresome and frought with peril. Can you tell a little about what they do? When I mentioned a single artefact, i meant some construct that might contain all the elements of your global list in a single searchable or indexable gadget.

I think that this should be addressed sooner rather than later.

Craig
Last edited by dunbarx on Mon Nov 30, 2015 6:43 pm, edited 1 time in total.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Stack Global Variables - How Bad are they? - Solved

Post by jacque » Sun Nov 29, 2015 6:27 pm

In the stacks I've seen, often globals are used because the author doesn't know how to pass parameters to other handlers. Passing parameters is a much better solution because the values only use memory temporarily and because it's much easier to see what they contain. If you are using globals for that reason then I recommend you use parameters instead.

I can't think of a reason why any stack would need that many globals. I hardly ever use them because they are difficult to track and remember what they do. They also use memory and have the potential of interfering with other stacks if they aren't very carefully named.

In general I try to avoid them. Also, just as a personal preference, I don't use custom properties for temporary values either, I use script local variables. That's mainly because it's too easy to accidentally save a custom property with the stack and get unexpected results on the next launch. Script locals aren't always possible but I aim for that whenever it can work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Stack Global Variables - How Bad are they? - Solved

Post by dave.kilroy » Sun Nov 29, 2015 8:16 pm

At the end of this link is a lovely scripting style guide from Richard Gaskin - well worth the read http://www.fourthworld.com/embassy/arti ... style.html
"...this is not the code you are looking for..."

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Stack Global Variables - How Bad are they? - Solved

Post by dunbarx » Sun Nov 29, 2015 8:33 pm

Jacque.

You and I are the only ones here who use the term "author".

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Stack Global Variables - How Bad are they? - Solved

Post by jacque » Sun Nov 29, 2015 9:45 pm

dunbarx wrote:Jacque.

You and I are the only ones here who use the term "author".

Craig
I was on a mobile device and "developer" was too long to tap out. ;-)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Stack Global Variables - How Bad are they? - Solved

Post by sritcp » Sun Nov 29, 2015 10:34 pm

jacque wrote:...... I don't use custom properties for temporary values either, I use script local variables. .......
When such temporary values need to be accessed across the system, global variables seem to me the best choice.
For instance, when the user is in "edit vs run mode" or "trial vs actual mode", the flag value might need to be accessed by more than one script.

To use a custom property for anything other than permanent (but variable) data associated with an object feels conceptually uncomfortable.

Regards,
Sri

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Stack Global Variables - How Bad are they? - Solved

Post by FourthWorld » Mon Nov 30, 2015 5:47 pm

DR White wrote:I have heard that one should not use Stack Global Variables...
You'll find dogma in all areas of human endeavor, and programming is no exception. :)

There are some who rather boldly assert that global variables should never be used, but it's worth noting that nearly every popular programming language supports them, suggesting we consider the possibility that the world of experts who design programming languages may not be entirely foolish.

In your case, as others here have noted, if you have a large number of globals perhaps a single global array may be best. Other alternatives may also be useful, each with its own strengths and weaknesses. I've outlined some of them here:
http://forums.livecode.com/viewtopic.ph ... 57#p129457

In short, global variables are no better nor worse than any other language feature. Like all features of all languages, it's possible to use them in contexts where an alternative may be better, but that doesn't mean they're not a good choice in some circumstances, or that any given alternative is the best choice for all possible circumstances.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply