Michel,
>What do you use instead of private variables ?
The worst problems come from code where variables are undeclared as was the typical practice with Clipper. Here is an example.
Code: Select all
function main()
use invoice // contains field named "TOTAL"
dowhatever()
return nil
function dowhatever()
total:= 10
msgInfo(total,"total") // returns 7.00
return nil
Total in the above routine is returning the value in the fieldname TOTAL not the private named total. This is very confusing.
In this example we declare the variable as a LOCAL.
Code: Select all
function dowhatever()
LOCAL total
total:= 10
msgInfo(total,"total") // returns 10.00
return nil
Now total returns the expected amount.
There is almost always an open file in the current workarea so there is a constant risk the fieldnames will override privates. If you add a field to a file, it could end up breaking your code. Conversely, you could add a PRIVATE that could end up breaking your code. These kinds of bugs are very hard to find since they may only occur under certain circumstances (when a certain file is open and in the current workarea).
My solution? I program extensively using OOP. My apps are also classes.
Code: Select all
class TApp
data ...
method New()
method Activate()
...
endclass
Then all you need to run your app is:
Code: Select all
function Main()
TApp():new():activate()
return nil
So anywhere in the app I can refer to data of the class.
And I create business classes, like customer, item, invoice, etc. Within these classes I have all the needed code related to the real-world item. This includes all the data as class data so no privates are needed.
I also use LOCALs and declare all variables. I compile with the \w parameter to ensure that all variables are declared.
You can also eliminate publics. The App class handles some requirements and you can use a set/get function to store any variable you need. A set/get function stores the data as a STATIC.
Set the path.
path( cPath )
Get the path anywhere in your code.
cPath := path()
Regards,
James