Jump to content

Should Helper Functions Be In The Model (MVC)?


DeX

Recommended Posts

I have models for all of my database objects. For instance, I have a User class and in that class I have functions like so:

getFirstName()

getLastName()

getAddress()

And of course I have setters for each as well. My question is whether I should also have some helper functions inside this model for something like getFullName() which will return a concatenation of getFirstName() and getLastName() or if we should simply concatenate the data every time we want to display it on each page. So we will be displaying the user's name on the top of each page, in their account profile and maybe some other places of this portal type system.

Is it proper object oriented programming to use helper functions like this? Should they go into the controller? Should we create separate logic namespaces for each model to control this logic?

Another example would be if I wanted to get the most recent user that signed up, where would this function go? In the user model? In the controller?

Thanks.

Link to comment
Share on other sites

1 hour ago, DeX said:

My question is whether I should also have some helper functions inside this model for something like getFullName() which will return a concatenation of getFirstName() and getLastName()

Does it make sense for the model to have a method related to its data?

1 hour ago, DeX said:

or if we should simply concatenate the data every time we want to display it on each page.

Does it make sense to do the same thing over and over again?

1 hour ago, DeX said:

Is it proper object oriented programming to use helper functions like this? Should they go into the controller? Should we create separate logic namespaces for each model to control this logic?

Code belongs in the one, single place (hopefully) that it is most related to. If the name is related to the model then it should be the model that deals with the name. If code is related to the controller, that being the place where you have code to deal with requests and responses and deciding what to render, then that is where it should go.

1 hour ago, DeX said:

Another example would be if I wanted to get the most recent user that signed up, where would this function go? In the user model? In the controller?

Is finding the most recent user something that relates mostly to the data managed by your model or to process managed by the controller?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.