Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Always better to ...?
Message
 
To
02/03/2000 19:06:06
Mike Yearwood
Toronto, Ontario, Canada
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00338054
Message ID:
00341220
Views:
30
>And look at my initial example. I found it in a commercial piece of code. The way I re-wrote the code was certainly easier to read and as I said, it wasn't any faster. I'm trying to say we need the best way to write code.
>
>Thanks

Mike,

Your example touches on something near and dear to my heart< g >. One of the first thing I assignments back (a number of years ago) in school was to write a function (in Pascal) to open a file. In considering this years later, I realized that there were a number of purposes to this assignment. One, this is a task that virtually every application has to do. Two, it teaches two of the principles mentioned in my other post (functional cohesion and loose coupling). But I digress.

It does remind me (the orignal) of some code I wrote, that I used back in FP 2.x. Since it does so, I'll couch this in terms of my logic at the time, which, BTW, I still feel is valid.

I wrote a function that had the purpose of opening one table. Like the function I had written back in school, it returned a Boolean value. What follow's is the code from that function (with comments added explaining the logic behind each step and updated to VFP syntax and conventions).
FUNCTION Open_Table

LPARAMETERS tctable, tcalias
* tctable is the fully qualified file name
* tcalias is the alias to be use and is optional
LOCAL llresult, lnargs, lcalias
llresult = .F.
lnargs = PCOUNT()
IF lnargs = 1
  lcalias = JUSTSTEM(tctable)
ELSE
  lcalias = tcalias
ENDIF
* Make sure that the file exists
IF FILE(tctable)
  IF USED(lcalias)
    SELECT (lcalias)
  ELSE
    SELECT 0
    USE (tctable)
  ENDIF
  * Was the table opened successfully?
  llresult = USED()
ENDIF
RETURN llresult
Admittedly, there are some more things I should do here. For example, what if the table's opened already under a different alias? That's mot addressed. Perhaps I might want to prompt the user if the file isn't found.

The point here, however, is two fold. First, and to me most important, what if we're unable to open the file? What if it's corrupted or used exclusively by another user. The code sample you provided doesn't take that possibility into account. The other purpose is this, once code such as this written (with the appropriate modifications, of course) and debugged, it never has to even looked at again. Anytime we need to open a table, we call the function passing the appropriate parameter(s). In fact, I recently told someone that I hadn't written code for a project to open a table in years.

Now the question may exist what about the error, should it occur. The answer is that this function shouldn't care. That's the error handler's responsibility, so neither the module that called the function nor the function itself should have to worry about anything other than success or failure and should be designed as such.
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform