domenica 5 febbraio 2017

Don't write your own template engine!

We all have done it at least one time: we designed our very-own sucking not-scalable template engine!

I remember when I was a young stupid developer, during my very first job as a contractor, I had to extract some data out of a database (not relational!) and to produce a well formatted PDF letter.
At that time my knowledge was really tiny, but luckily I had in my bag of tools both Perl and LaTeX, so I decided to use both of them (and by the way it was years before OpenOffice too).

As I said I was young, and therefore as pretty much every "green" developer does, I refused to search for and reuse some production ready module or extension (Template Toolkit, just to name it), considering myself enough smarter to develop each required functionality by myself.

So I started developing a Perl engine able to read an INI-like file format: I was extracting data out of the database and placing each record as a set of sections into the file, with special sections used to control the engine itself. Having to deal with text, why not using Perl?
Having done the above, the second phase consisted of producing LaTeX compatible output and run a controlled process to compile the output and produce the result. Again, Perl could do of course the job.

And you can see a recurring error here: as I was not using an engine template on the "reading" side (parsing my very own INI-like file format), I was not using it in the "writing" side too (outputting each single LaTeX command one by one).

So far, so bad, I had a fully working system in less than two days (thank you Perl for regexp!), and my boss was happy with that, the check was in the incoming mail, and I was enough happy too.

I have to confess that the script resulted in around 500+ lines, due also to the fact I was not using "short" comparison operators as I do today (thank you '?:'), and of course maintaining the script was a really pain.

Anyway, I learnt the lesson and since those days I never started developing my very own template engine unless really forced to.
A few years after I had enough experience to recognize the very same error I did in code provided by other developers, and this let me thought that the problem was a cultural one. I cannot say if it is due to impatience or a lack in the preparation of young developers, I tend to balme the latter most since, having worked a few years in the academic field, I had seen too less time spent explaining the importance of a language ecosystem (thank you CPAN!).

Nessun commento: