Global declarations
Moderator: Klaus
Global declarations
Why, after 28 years, is it still necessary, or desirable, to have to declare globals in each script they are used? Is there an advantage to doing that? I suppose that one might re-use a global's name locally in a handler, if not declared in its script, but that seems like poor style, and hardly an advantage.
So, anyone love this? Is it hard to change? Is there any legacy code that would break? Would a new global property, the "globalGestalt" be useful to keep the old way or permit the new?
Craig Newman
So, anyone love this? Is it hard to change? Is there any legacy code that would break? Would a new global property, the "globalGestalt" be useful to keep the old way or permit the new?
Craig Newman
-
- Livecode Opensource Backer
- Posts: 366
- Joined: Tue Apr 10, 2012 9:18 am
Re: Global declarations
This has often confused me, but I just figured there was a good reason for it.
My first thought when I started using LC was I could declare my globals on the stack script, and then use the globals everywhere.
instead, I have to re-declare these globals on every card where I want to use them. Now that you bring it up, it would be so much easier if I just had to declare once.
My first thought when I started using LC was I could declare my globals on the stack script, and then use the globals everywhere.
instead, I have to re-declare these globals on every card where I want to use them. Now that you bring it up, it would be so much easier if I just had to declare once.
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Global declarations
LiveCode has no main.c. So if you declare a global in one script, and use a local of the same name in another, which one wins?
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
Re: Global declarations
As a new user, I find it weird that a global variable has to be declared again and again.dunbarx wrote:Why, after 28 years, is it still necessary, or desirable, to have to declare globals in each script they are used? Is there an advantage to doing that? I suppose that one might re-use a global's name locally in a handler, if not declared in its script, but that seems like poor style, and hardly an advantage.
So, anyone love this? Is it hard to change? Is there any legacy code that would break? Would a new global property, the "globalGestalt" be useful to keep the old way or permit the new?
Craig Newman
You ask if anyone loves this? I don't!
Re: Global declarations
First off, global variables should be a last resort.LiveCode has no main.c. So if you declare a global in one script, and use a local of the same name in another, which one wins?
Secondly, a good naming convention should arbitrate between global and local variables.
Thirdly, the compiler should barf (and *usually* does) and refuse to compile when finding a mismatch.
You might equally ask, if you declare
constant pie = 4 # in one stack and
constant pie = 3 # in another stack,
which one is right?
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Global declarations
Mark.
I agree with all three points. But what do you think about the basic premiss? Is there a downside to having the ability to declare a global once, er, globally?
There is an advantage (for me) to see required declared globals within each script. This seems like the only upside; a "global" global declared somewhere else would not be visible, and if you have to list them in each script for reference, you may as well declare them instead. How do other languages deal with this issue? I use custom properties mainly, and these are indeed "hidden", in the sense that when required to be referenced, you just have to know they exist. I have never listed these at the head of a script, though it all of a sudden seems like a great idea.
Craig
I agree with all three points. But what do you think about the basic premiss? Is there a downside to having the ability to declare a global once, er, globally?
There is an advantage (for me) to see required declared globals within each script. This seems like the only upside; a "global" global declared somewhere else would not be visible, and if you have to list them in each script for reference, you may as well declare them instead. How do other languages deal with this issue? I use custom properties mainly, and these are indeed "hidden", in the sense that when required to be referenced, you just have to know they exist. I have never listed these at the head of a script, though it all of a sudden seems like a great idea.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Global declarations
Agreed on all counts. I merely noted why the original language designers probably chose to handle global declarations as they did. Whether it's "best" or even truly necessary is of course debatable, but the one thing we know for sure is that we have more than two decades of code based on the current declaration requirements, so I wouldn't expect to see this changed anytime soon.mwieder wrote:First off, global variables should be a last resort.LiveCode has no main.c. So if you declare a global in one script, and use a local of the same name in another, which one wins?
Secondly, a good naming convention should arbitrate between global and local variables.
Thirdly, the compiler should barf (and *usually* does) and refuse to compile when finding a mismatch.
You might equally ask, if you declare
constant pie = 4 # in one stack and
constant pie = 3 # in another stack,
which one is right?
Besides, for all the reasons you noted, the more experience one has with LiveCode the less this is an inconvenience. Over time we use globals ever less frequently, we get increasingly mindful about clearly-readable naming conventions, and we tend to factor code in ways that minimizes use of globals across multiple scripts.
LiveCode Builder, however, may be a different matter. This may be useful to raise in that forum to simplify declarations in that new language.
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