D
D
dipwoodle2015-01-30 22:05:40
JavaScript
dipwoodle, 2015-01-30 22:05:40

Format date without library. Just a couple of features per project. What architectural solution to choose?

In the project, the date must be displayed in two formats:
- From now - 4 min ago
- MMM, YYYY - Jan, 2014
Example:

Handlebars.registerHelper "date_format", (date, options) ->
    moment(date).format('MMM, YYYY')

  Handlebars.registerHelper "from_now", (date, options) ->
    moment(date).fromNow()

<i class="icon-clock"></i>
{{from_now date_posted}}
...
<li><span>Start Date:</span> {{date_format start_date}}</li>

The task is to cut out the moment library, because for the sake of two functions it is a lot to load 11.6k gz for the project.
Question: how is it architecturally more correct to do this?
Options:
1. Make a separate small DateUtil module, shove two custom formatting functions there. Add two view helpers that call the DateUtil functions. We call helpers in templates.
2. Do not make a separate module, write the formatting logic directly in view helpers.
3. Your decision.
Criticism of the proposed options
1.
- An extra module, if the formatting will not be used anywhere except for views
- Continuation of the paragraph above: an extra composition. The helper body simply calls the DateUtil function
- It is not clear how to name the method for the second date
type

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Smirnov, 2015-01-31
@dipwoodle

Answer the following questions for yourself:
1. How many dates will be added to the project in the near future (let's say 6 months).
2. Which of the options I will write faster.
3. Will it be difficult to refactor if I write simple now, and then I need a more complex system.
In your case, this is a trifle, and not some major architectural decision. Of course, you shouldn’t lose sight of the little things either, but in such cases the most correct option is to do it because it’s faster and there is less code, then if you need to refactor.
And yes, the first question is the most important here. Because we design not to be beautiful, but to change the code later without any problems. You always need to think about what changes can happen.
THOSE. In your case the answer is probably 2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question