Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Refactoring date/time functions
Message
General information
Forum:
Visual FoxPro
Category:
Refactoring and unit testing
Title:
Refactoring date/time functions
Miscellaneous
Thread ID:
01035519
Message ID:
01035519
Views:
61
In a recent discussion on creating a primary key, the issue came up of how to convert a date to a string. As is common, VFP provides multiple solutions for the problem. I would like to add some observations on this from a refactoring point of view.

The first and most obvious function for convering a date to character string is
DTOC(datefield)
This will return a caracter value for the date in the form MM/DD/YY. The exact format of the date will be dependant on your settings for
SET DATE
and
SET CENTURY
If as an alternative, you use
DTOC(datefield,1)
The date will always be returned as an 8 character string in the form
YYYYMMDD
This field is not very pretty for display purposes, and I would not use it for that purpose, but it is excellent if you need to create a character string that includes the date along with other character data.
From the point of view of program clarity, however, I think that the ',1' greatly decreases the clarity of the code. When I look at the line
DTOC(datefield,1)
One of 2 things happen to me, I either miss the ,1 and read it as
DTOC(datefield)
Or I open my help file to figure out what the ,1 does. Either way, this is not clear and clean code. If I need an 8 character, sortable date in a string format, I would use instead
DTOS(datefield)
This would return my 8 character YMD string regardless of the settings for
Set Date
and
Set Century
We are good so far, but now we run into a problem with consistency with time commands. The following commands are valid
TTOC(DateTimeField)
TTOC(DateTimeField,1)
But, there is no command for
TTOS(DateTimeField)
Thus, while we could use DTOS, there is no corresponding TTOS. My solution here would be to create a new function as follows
Function TTOS(DateTimeField)
Return TTOC(DateTimeField,1)
By using my TTOS instead of the built in TTOC(DateTimeField,1) function, I mirror the DTOS function for clarity and consistency, while hiding the ,1 parameter from day to day use.
In fact, I would probably go a step further. The whole point of refactoring is to create clear and easily readble code. Unless you are very familiar with the date/time functions, the acronyms can be obscure. This could easily be resolved as follows
DTOC(DateField)
becomes
Function DateToCharacter(DateField)
return DTOC(DateField)
DTOS(DateField)
becomes
Function DateToString(DateField)
return DTOS(DateField)
With the expanded names, the purpose of the functions becomes more clear, and the code becomes more easily readable and maintanable. Any parameters are moved down the code tree, where they can be ignored on a day to day basis.
Once these new functions are built and tested, they would just be stored in a library and available for use in any future coding.
Burt Rosen
Software News & Views, Editor in Chief and Writer
Next
Reply
Map
View

Click here to load this message in the networking platform