1

I haven't done a lot of coding in dynamically typed languages such as JavaScript until recently, and now that I'm beginning to understand what's possible, I'm starting to wonder what's a good idea and what's not. Specifically, I am unsure as to whether changing the type of a variable as a function progresses through a sequence of operations is considered good practice.

For example, I have a bunch of files containing dates as strings. I'm using front-matter to extract date attributes and storing them within an object representing the original file. The strings themselves aren't very consistent, so I'm then using Moment.js to parse them, and storing the result back in the same attribute in the same object article.date. This feels more or less right to me, as article.date is only a String for one operation before being parsed and stored as a Date/'Moment' type.

The part I'm a little unsure of comes next. This is part of an ExpressJS app, and so an array of these objects is getting passed in as the data in a render() call, where it goes to a Jade template for rendering. But what if I want to use a display method in Moment.js to control the way the date looks as a String? Would it be reasonable to change the type of the date attribute back to a String again before passing it in?

Example:

articles[i] = processArticle(content); 
// creates an article object from YAML, object has a property article.attributes.date
articles[i].attributes.date = moment(articles[i].attributes.date); 
// attribute is now a Date/Moment
articles[i].attributes.date = articles[i].attributes.date.format("dddd, MMMM Do YYYY, h:mm:ss a");
// attribute is now "Sunday, February 14th 2010, 3:25:50 pm"
Toadfish
  • 1,112
  • 1
  • 12
  • 22
  • 1
    My pref would be not to do that. If you write some other intermediate method, how do you know if `attributes.date` is a String or not? I don't love Hungarian notation, but in this case i would call one `dateString` and the other `date` or `moment`. – user949300 Feb 05 '16 at 05:43
  • Some nit picking :-) Variables and properties don't have a type, values do. Here you are dealing with object properties, not variables. You might consider an extra property, say *dateString*, that is your formatted string. The biggest issues with converting from string to date and back sequentially is performance (wasted cycles) and whether you consistently parse and format the string without changing the value it represents. Only you know the answers. ;-) – RobG Feb 05 '16 at 05:46
  • @RobG Sorry, noob counter-nitpick/question: Is this because we're talking about a dynamically typed language? i.e. a variable/property doesn't have a type, because it can contain a *value* of any type? Or is this a fundamental concept? I'm thinking in terms of how in a statically typed language `int foo = 100` declares `foo` to be an int, and then assigns it a value (which had better be an int, or else) – Toadfish Feb 05 '16 at 05:53

1 Answers1

1

The reason why we even have type safety is to find errors in code early on: preventing invalid memory access/illegal operations/etc. In object orientated languages to allow decoupling and code re-use (polymorphism). Not just to make your life as a programmer more difficult, but to make sure your program will run.

Because JavaScript does not provide type safety, it's up to the programmer to do this. You will have to make sure that operations on a variable are valid and do not lead to an exception which stops your program from running. You will have to make sure that a method call can be invoked on any object.

So to answer your question: no, it's not a good practice to change the type of a variable as a function progresses.

To use Moment.js in your Jade template: How do I display todays date in Node.js Jade?

Community
  • 1
  • 1
Sonata
  • 2,177
  • 25
  • 32
  • Just to confirm, does this mean that #{articles.attribute.date.format("dddd, MMMM Do YYYY, h:mm a")} would reformat the moment date for display at render time from within the Jade template? I've been keeping all my logic on the JS side and just passing in strings for jade to slot into a template, because I'm more confident in JS than I am in jade and it seemed like the most sensible division of labour. – Toadfish Feb 05 '16 at 12:59
  • 1
    Yes. I would consider doing formatting in the template to be better: in JS you are interested in objects (e.g. moment object) and not their representation for display/output. In the Jade template however, you are and therefore the formatting belongs there. – Sonata Feb 05 '16 at 14:52
  • Do i need to do anything special to be able to access moment.js from inside the jade template? Or does it look in the app.locals of the express side of things without any intervention on my part? Your link implies the latter but I didn't think Jade would be quite that tightly integrated, I'm learning to give Jade a lot more credit I think. – Toadfish Feb 05 '16 at 14:56
  • 1
    Just adding it to the app.locals as described is enough, see [express doc](http://expressjs.com/en/api.html#app.locals) and [Making use of utility libraries in server-side Jade templates](https://coderwall.com/p/egh53a/making-use-of-utility-libraries-in-server-side-jade-templates). You can give Jade credit. Template-engines can be a valuable tool, too! – Sonata Feb 05 '16 at 15:02