Should dates and currencies always be passed around as objects?
Help me settle an argument with a coworker...
We’re using Zend Framework and PHP and much of the data we deal with includes dates and prices. I’ve been arguing that when the data is retrieved (either from the database or JSON) we should automatically immediately convert the date properties to Zend_Date objects and the price properties to Zend_Currency objects so as to ensure that we are always using the abilities of those classes when manipulating/comparing/displaying the data. My argument is that it ensures consistency and we always know what form the data is in.
My coworker argues that we should just leave the data as is (strings) when retrieved and only create the Zen开发者_开发技巧d_Date/Zend_Currency objects when used in the model or view.
Who is right and why?
(I’m sure someone will argue that there is a small speed optimization penalty in creating the objects, but in our case that is not of concern.)
In OO the best practice would surely be to use objects as they encapsulate the state and behaviour of what you expect a currency/date to be.
I am not experienced with Zend but I would assume the date class would do some checking to ensure that the date is a valid date or throw an exception. If you wait until the last minute to convert the date to an object you are potentially making it harder to find the source of the bug and transporting around tainted data the whole time. My defensive nature would want me to sanitize data as soon as possible that has not come from a known trusted source.
精彩评论