Global declarations

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6160
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Global declarations

Post by dunbarx » Tue Jul 28, 2015 11:28 pm

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

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 312
Joined: Tue Apr 10, 2012 9:18 am

Re: Global declarations

Post by ghettocottage » Tue Jul 28, 2015 11:49 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Global declarations

Post by FourthWorld » Wed Jul 29, 2015 12:32 am

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
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Global declarations

Post by golive » Wed Jul 29, 2015 1:13 am

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!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2826
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Global declarations

Post by mwieder » Wed Jul 29, 2015 6:49 am

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6160
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Global declarations

Post by dunbarx » Wed Jul 29, 2015 1:14 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Global declarations

Post by FourthWorld » Wed Jul 29, 2015 2:50 pm

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.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Feature Requests”