Stack Global Variables - How Bad are they? - Solved
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Stack Global Variables - How Bad are they? - Solved
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
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.
Re: Stack Global Variables - How Bad are they?
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
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.
Re: Stack Global Variables - How Bad are they?
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
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
Re: Stack Global Variables - How Bad are they? - Solved
Hi David,
100 global variables?
I would use an ARRAY instead (global or in a custom property), easier to fill, manage and access
Best
Klaus
100 global variables?

I would use an ARRAY instead (global or in a custom property), easier to fill, manage and access

Best
Klaus
Re: Stack Global Variables - How Bad are they? - Solved
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
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.
Re: Stack Global Variables - How Bad are they? - Solved
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.
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
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 858
- Joined: Wed Jun 24, 2009 1:17 pm
- Contact:
Re: Stack Global Variables - How Bad are they? - Solved
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..."
Re: Stack Global Variables - How Bad are they? - Solved
Jacque.
You and I are the only ones here who use the term "author".
Craig
You and I are the only ones here who use the term "author".
Craig
Re: Stack Global Variables - How Bad are they? - Solved
I was on a mobile device and "developer" was too long to tap out.dunbarx wrote:Jacque.
You and I are the only ones here who use the term "author".
Craig

Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Stack Global Variables - How Bad are they? - Solved
When such temporary values need to be accessed across the system, global variables seem to me the best choice.jacque wrote:...... I don't use custom properties for temporary values either, I use script local variables. .......
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
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Stack Global Variables - How Bad are they? - Solved
You'll find dogma in all areas of human endeavor, and programming is no exception.DR White wrote:I have heard that one should not use Stack Global Variables...

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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn