Tip: Don’t set values without saving them first

I’ve been reviewing some FoxPro code recently and wanted to pass along a recommendation.

Don’t issue SET statements without checking the value first.

Many developers do this already and consider it obvious but many don’t or they end up writing extra lines of code for no reason.

For example, if you say SET EXACT ON , run some code, and then say SET EXACT OFF – what happens if the user already HAD SET EXACT turned ON?

You just affected someone else’s code without realizing it.

After all, Why write

lc = SET(“EXACT”)
SET EXACT OFF

and then
SET EXACT &lc

At the end of the code when you can get by with just one line of code.

Now to deal with this, I’ve created a piece of code that deals with this in a single line.

DEFINE CLASS csaveset AS ffcustom

csetting = .F.
cvalue = “”
Name = “csaveset”

PROCEDURE Destroy
LPARAMETERS tcSetting,tcValue
LOCAL lcSetting
lcSetting = THIS.cSetting
lcValue = THIS.cValue

IF NOT EMPTY(lcSetting)
SET &lcSetting &lcValue
ENDIF
ENDPROC

PROCEDURE Init
LPARAMETERS tcSetting,tcValue

THIS.cValue = SET(tcSetting)
THIS.cSetting = tcSetting

SET &tcSetting &tcValue
ENDPROC

ENDDEFINE