Saturday, December 17, 2005

Fighting hard coding

Hard coding is not a good thing. Sooner or later the name of the company, the number of tutor groups, the web address of the program's support page, etc, will change.

One solution is to put the information in an ini file. Many people scorn ini files, I don't.

Another solution is to put the information in a table. My table has two fields: cname and cvalue.



select cname, cvalue from (ctable) into array a_temp

CLOSE databases

for i = 1 to _tally
cdatatype = left(a_temp(i,1), 1) && c, l or n
do case
case cdatatype = 'c'
this.addproperty(alltrim(a_temp(i,1)), alltrim(a_temp(i,2)))
case cdatatype = 'l'
this.addproperty(alltrim(a_temp(i,1)), iif(ALLTRIM(lower(a_temp(i,2))) == '.t.', .t., .f.))
case cdatatype = 'n'
this.addproperty(alltrim(a_temp(i,1)), val(a_temp(i,2)))
case otherwise
MESSAGEBOX("Corrupt " + ctable + ". The program will end.", vbokonly + vbinformation, "Error")
RETURN .f.
ENDCASE

ENDFOR

The first character in cname stores the data type. The code is written in Visual FoxPro 8. Each piece of data is stored as a property in an object I have open as long as the program is running, making them behave like global variables.

I think this is a clever way of dealing with data of this kind. Even better, I think it is my own invention! :) At least, I can't remember I read about it somewhere.

No comments: