Page 1 of 1

Global declarations

Posted: Tue Jul 28, 2015 11:28 pm
by dunbarx
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

Re: Global declarations

Posted: Tue Jul 28, 2015 11:49 pm
by ghettocottage
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.

Re: Global declarations

Posted: Wed Jul 29, 2015 12:32 am
by FourthWorld
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?

Re: Global declarations

Posted: Wed Jul 29, 2015 1:13 am
by golive
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
As a new user, I find it weird that a global variable has to be declared again and again.
You ask if anyone loves this? I don't!

Re: Global declarations

Posted: Wed Jul 29, 2015 6:49 am
by mwieder
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?
First off, global variables should be a last resort.
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?

Re: Global declarations

Posted: Wed Jul 29, 2015 1:14 pm
by dunbarx
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

Re: Global declarations

Posted: Wed Jul 29, 2015 2:50 pm
by FourthWorld
mwieder wrote:
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?
First off, global variables should be a last resort.
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?
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.

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.