Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can Default Scope of Variables be Changed to LOCAL?

Status
Not open for further replies.

Steve Meyerson

Programmer
Sep 17, 2020
319
US
As a convenience, I'd like to avoid having to list all local variables near the top of all my methods.

I don't use PRIVATE (default?) or PUBLIC. Instead, I always create properties instead of creating variables which would be declared as PUBLIC or PRIVATE.

Thanks.

Steve
 
Steve, the short answer is No, you can't change the default scope. And if you could, I would advise against it. If you were able to write code such that, if a variable is not scoped, it is automatically LOCAL, that might suit your purposes, but it would confuse the heck out of anyone who has to maintain your code, and could lead to all kinds of rannygazoo.

What I suggest you do is to find a tool that automatically looks for missing variable declarations, and automatically puts a LOCAL in place for each one it finds. One such tool is Maurice De Beijer's PareLocals (see I've been using it for years, and it does a good job, although it does have some limitations. (If you can't find it by following the above link, look for it on GitHub.)

I believe there is a similar tool in Thor, but I have not had a chance to try it.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I don't think that's possible, it's meant to be part of the discipline to list the variables before they are used.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Thanks guys. I'll look at the tool you recommend. It may come in handy since occasionally I'll create 30-60 variables with field names to insert a record with the short GATHER FROM MEMVAR command.

Steve
 
Not sure if this will help, but if you use the [tt]NAME ObjectName[/tt] clause with SCATTER / GATHER, you don't have to worry about individual variables. The values are all held in properties of the object.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike said:
Not sure if this will help, but if you use the NAME ObjectName clause...
Yes it helps. It is (now) an obvious solution which hadn't occurred to me. Thanks.

Steve
 
As Mike thought, there's a Thor tool that reads your code and creates LOCAL definitions for every undefined variable. You can configure it to the style you want.

Tamar
 
Private variables are almost like local ones, if you precede every function and method with PRIVATE ALL. But that also contradicts the purpose of private scoped variables to be available in called methods

As it's all about SCATTER/GATHER MEMVAR, the better option is to make use of the NAME object clause.

And you can also regulate array created by A..() functions (like ALINES(), ADIR(), etc) to be LOCAL by predefinig them as 1 element arrays, as all these array functions extend an existing array variable instead of creating a private one. So it's really not hard to have local scoping.

And I'd not consider it a burden to have a LOCAL statement. You can use the LOCAL statements to have a comment about the meaning of the variables and so use this best practice and also document your code at the same time.

One warning about SCATTER/GATHER... No matter if you use MEMVAR or NAME: You're hiding the details here, the code does not show what fields are available and copying over data this way from one table to another similar structured or even into the same table is a bit suspicious anyway, you should just need to refer to the already stored data with a reference - a foreign key field. Exceptions confirm the rule, if the cpied data changes in a process over time, and should be independent from the origin it's obviously okay to copy over something.

One clever use of an object variable can be to scatter the order id from an order record to all orderitems/details while working on an order, as this copy of the id as foreign key is what's supposed to be copied. It's also clever to have a tbale wih Width,Height,Top, Left fields and scatter them to a form object to store and restore form position and size. And in this case you also will need to break the rule to have reserved words as field names.

Chriss
 
One more note: Private variables different from local variables in a really important way. Declaring a variable as private does NOT create it or initialize it. Declaring a variable as local (or public) creates and initializes (to .F.) that variable.

Tamar
 
Olaf taught me that a few months back - well worth knowing

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top