1

I've got a model which has a getDate() getter method.

In the controller I defined a utility class called timeAgoUtil which calculates the time since that Date value, for example: 6 hours ago.

Now I need to access that utility method every time I want to get the value on screen, which is quite a lot. Most of the times on objects in a list, which requires me to loop through it to get each time ago value.

My approach:

  • At the start of the model I defined the timeAgoUtil utility class.
  • I created a new getter method to the model called getTimeAgo().
  • In that method I put return timeAgoUtil.convert(this.date);.

Is manipulating the result before retuning it in a getter AND defining a utility class inside the model common use?

Kara
  • 6,115
  • 16
  • 50
  • 57
Tom
  • 869
  • 6
  • 25
  • I believe it should be ok to have this logic in your getter method. Since it has high usage why don't you do it in front end? – TheCodingFrog Jan 20 '16 at 17:42

2 Answers2

1

No it is not.

Getters should ideally return the data only and should not contain any business logic. That is the general approach and the best practice.

What you are doing now is the not the right approach as well, though it will work.

The best approach/practice would be to define a Utility class and inside that define the method, which calculates the time. Model should not contain any other methods other than setters, getters, constructors, and if required equals and hashcode.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • `"Model should not contain any other methods other than setters and getters."` -- I'm not quite sure that I agree with this statement. Edit: or even with your edited addition of `"..., constructors, and if required equals and hashcode."`. This would make for a severely limited use model. – Hovercraft Full Of Eels Jan 20 '16 at 17:42
  • @HovercraftFullOfEels Ideally you should have only these setters, getters, constructors, and if required equals and hashcode. Isnt it ? And ideally business logic should not be there in models. Right ? – Pritam Banerjee Jan 20 '16 at 17:43
  • Per the [Wikipedia article on MVC](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller): "The central component of MVC, the model, captures the behavior of the application in terms of its problem domain, independent of the user interface. **The model directly manages the data, logic and rules of the application**." – Hovercraft Full Of Eels Jan 20 '16 at 17:48
  • You can have business logic in Models, but ideally you should not. That is what I know. – Pritam Banerjee Jan 20 '16 at 17:53
  • I do have defined a Utility class which has a method which does all the calculation. I'm declaring that utility class inside the model, from which I make use of it's method inside the getter method. – Tom Jan 20 '16 at 18:08
  • Is there any particular reason of defining that Utilty class inside that model ? If it's only just one method then you can have it in Controller. So when the data comes to the controller, you call that method and then pass it on to the front end. That would be your best bet according to me. – Pritam Banerjee Jan 20 '16 at 18:11
0

By the information you've provided, it looks like you are using the getDate() method to get an object's date and figure out how much time has passed since that date. In my opinion, you shouldn't do this in backend.

My suggestion would be to do this in front end either using plain js or a framework like angular

EDIT

As mentioned here you can use what Stack Exchange sites use the to create these time strings. There is this jQuery plugin called timeago

It's quite easy to use the plugin, and it's clean and updates automatically.

Here's a quick sample (from the plugin's home page):

// Now, let's attach it to your timestamps on DOM ready:
// This will turn all abbr elements with a class of timeago and an ISO 8601 timestamp in the title: 

jQuery(document).ready(function() {

  jQuery("abbr.timeago").timeago();
});
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17,
  2008</abbr> into something like this: <abbr class="timeago" title="July 17,
  2008">about a year ago</abbr> which yields: about a year ago. As time passes, the timestamps will automatically update.
Community
  • 1
  • 1
George Artemiou
  • 3,076
  • 2
  • 19
  • 36
  • Seems like in this case that is the best way to go. But this does not truly answer the question about having a Utility class declared inside the model and using a method of it inside a getter. I still want to thank you for your helpful answer. – Tom Jan 20 '16 at 21:10
  • In my opinion, getters and setters should not perform any data manipulation operations, that's why you have your service layer, to apply any business logic you want on the model returned from your daos. As for defining a Utility class inside the Model class, that breaks the Model class principle, i wouldn't do it but if it works for you, you should use it. There is no right or wrong, it always comes down to how you would like to structure your code. I told you what I would do if I were you so it's up to you :) – George Artemiou Jan 20 '16 at 23:06