Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose that you want to show a user list, on a web application having to handle

ID: 650595 • Letter: S

Question

Suppose that you want to show a user list, on a web application having to handle custom date formats (depending on current selected interface language).

Several locations for applying the date format seem possible. Which one would you use, and why?

At model, for example doing a DATE_FORMAT(user.birthdate, '%d/%m/%Y') for date fields (supposing we have a MySQL DB).

At controller level, for example (supposing we use PHP):

$users = UserModel::getUsers();
// loops through results before display to format dates.
// in the $users structure, we have to know which fields are dates or not
$users = MagicFunctionFormattingDates($users);

$view = new View('someTemplate');
$view->assign('users', $users);
At view level, for example (supposing we use a template language such as Smarty):

{foreach key=keyitem item=row from=$users}
<tr>
{foreach key=keyfield item=field from=$row.fields}
     {* Here $field should be an Object or associative array
        holding its 'type' (is it a date or something else) *}
     <td>{MagicFunctionForFormatting field=$field}</td>
{/foreach}
</tr>
{/endforeach}
A similar and related question goes for sumbitting dates (e.g. on a user registration form). Where should the date conversion take place:

In the controller processing and validating the form, before calling the model?

In the model?

Explanation / Answer

I'd suggest two things:

The model is where your database I/O happens. In that case you want everything going in and out of the database to be a a predefined form. This is your "neutral" form, the defined interface to everything else which must always be consistent.

After getting 1 clear, you can do transforms somewhere else. Where else is up to your design.

HOWEVER... in thinking about this a bit more, suppose the date format changes. Suppose, further, that you allow date format to be presented according to user preferences.

In that case you would seem to have 2 choices:

Model: the interface is in the users preferred format. OR

Model: the interface is always the neutral format, in which case, VIEW should probably be where you transform to/from the way that the user gets to see it.

Going with option 2, this means that controller can also always work with the neutral format meaning that coding functions/operations in it might be easier and/or less prone to change or reworking.

However, this does all depend on what you are trying to achieve.

My final take on all this: keep to a neutral form as long as you possibly can and then convert to/from the user preferred form as late as you possibly can. (And this probably means View is where your transforms should take place).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote