REPLACE FOR ErrorIn(REPLACE)

REPLACE is one of those 174 commands in Visual FoxPro that looks easy at first glance. The only easy thing about REPLACE, though, is the easiness of introducing subtle errors in your code. A well known behavior of REPLACE is that a line like

REPLACE alias.field WITH value

What makes this bug so hard to spot is that it's a context dependent error. All you need is one routine that changes the current work area but doesn't restore it properly combined with one routine that uses this REPLACE line without activating the correct work area. Suddenly you have the error, but only when the user triggers the code in a specific order.

Obviously that's not an error you want to have in a COM server or unattended application. Maybe that's a good time to point to the relatively new command


SET TABLEPROMPT OFF

Which doesn't fix the error, but at least doesn't hang your application with a file open dialog when you encounter the problem.

[UPDATE] As has correctly been pointed out: This issue can only become a problem if you omit the IN clause.
Once you switch to using


REPLACE field WITH value IN alias

or any other combination that includes the IN clause, the issue is non-existent. Well, you can still introduce errors
by specifying empty work areas, but those bugs are much easier to spot.