Template-Based Programming

12/10/2018 10:25:45

Last year I released Magnum, a C implementation of the Mustache template system.

I initially became interested in Mustache because I thought it would be interesting to combine with MultiMarkdown in order to allow shortcuts to quickly create longer documents that contained repetitive sections based on external data. For example, if you were writing a dictionary, and had a database containing words and their definitions, you could create a template that would combine the information from the database with MultiMarkdown syntax in order to create the final document. Think of it like “mail merge” on steroids.

It quickly occurred to me, however, that the range of potential applications was much, much broader.

In computer programming, you often come across situations where you have multiple sections of code that are almost, but not quite, exactly the same. For example, if you are working with data stored in a SQLite database, you may want to read data from the database and create a structure in C in order to store that data. If your database contains multiple tables, you may need to create multiple structures so that one structure exists for data from each table. The tables and structures are different, but the process of converting from one to the other is very similar across them all.

One possibility is to write a single chunk of code that can handle all edge cases across all tables/structures. In object-oriented programming, you could create one object and then subclass is for each table/structure pair. An advantage of this is that the code code may be something that can be shared with other programmers and other projects, which is great. A disadvantage is that it can be slower to develop, and may be slower to run as well, since it will add further complexity.

For me, it seemed that a simpler solution would be to create Magnum-based templates that would convert an abstract definition of an “object” (contained in a JSON file) into multiple files:

  • A shell script to create a SQLite table
  • A .c file and .h header to provide C functions to allocate and free “objects” as well as convert back and forth to SQLite data
  • Whatever else you need – this can be applied to any text-based programming you desire

For me, this has been fantastic. I have used it to quickly prototype data structures for new ideas. I have used it to standardize repetitive parts of other projects (for example, creating the code necessary to manage settings tables in iOS applications). If I find an error in my fundamental algorithms, I can update the master template, and then immediately update all of the generated forms so that they include the fix. More importantly, if I decide that I need to change the underlying data structure, it is trivial to update all of the related files so that the changes are shared across the SQLite and C code that I use.

I think the “sweet spot” of usefulness is in things that are too complex to hand write each one, but not so complex that it is worth writing a true “universal” library to handle all conceivable instances. Code that typically has a lot of “boilerplate” is likely to be a good candidate for this process.

Similar Pages