views:

249

answers:

4

I think the easiest way to ask this question is with the actual real-world situation I'm facing.

In our system, we have a Site model (classic MVC framework here) that represents a row in our site table. One of the fields of the site table that is stored in the Site model is the time zone of the site. We use this to adjust UTC datetimes from the database to the site's time zone on display. There are a few helper methods that aid in this: utcDatetimeToSiteTimestamp(), utcTimeStampToSiteDatetime(), etc.

Since these helper methods use the time zone of the site that's held in the Site model, is it okay to include these methods in the model? Or should they be placed in a separate helper class or something?

I know models should do more than just hold a row of data from the database, but where is the line between helper methods that act on this data vs. helper methods that use this data to act on external input?

Thanks for any insight!

A: 

I would create a helper class, since these methods don't really belong to one model. They'd be static methods, which in my opinion is a pro for the seperate helper class.

janoliver
+2  A: 

The answer lays in area of responsibility. A model class is responsible for knowing how to add stuff to it, modify stuff in it, remove stuff from it, and when & how to change it's internal state.
Nothing about conversion there. Converters belong external the class. So do formatting objects, or anything responsible for how the model gets represented to a client.

Jim Barrows
+2  A: 

This is a matter for some judgement. If the helper methods are general and may be useful in other classes then create a helper class since this makes the code re-usable. If the helper methods are very specific to the class in question then leave them right there.

Vincent Ramdhanie
A: 

I agree with Vincent Ramdhanie in that it's a matter of judgment. Personally I don't like to define helper functions from within my model classes - I think it's too messy, and that they simply don't belong there (plus I often find that barely any helper function only gets used once). However, that's not to say that I don't use helper functions from within my models. For example, if I have a BlogPost model class, my getOneCompiled() method would use helper functions to assemble some links before returning an array representing the table row.

It's important that you look at your options, and see which will result in code that's easy to read code and maintain. Writing the helper function in it's own file may require some more upfront work, but if it makes your code easier to manage, it's certainly worth it.

Arms