Template Oriented Programming
In computer programming, Template oriented programming (TOP) is a programming paradigm that focuses on templates to accomplish a programmer’s goals. Template oriented programming is a more general version of generic programming in which the template is the fundamental abstraction mechanism, as opposed to traditional generic programming applications that add templates to traditional languages such as C++, C#, or Java.
Introduction
The goal of template oriented programming is not to directly create machine code (or byte codes) to be executed on a (virtual) machine. Instead, the output of TOP is information to be processed by other code (or by humans). This could include familiar template output such as HTML/XML, or more complex code like C/C++/Java/C#, shell scripts, VHDL/Verilog, CAD scripts, etc. TOP allows one to generate one or more tedious artifacts from a simple, user defined model that is input.
Note that when the word template is used here, it is not talking about a specific programming language feature such as C++ template, C# generics, etc. Instead, it refers to the general concept of a template.
Many current template implementations of Template processors target text as their output. In general, however, template oriented programming may target code (such as C# generics), or any internal abstractions, such as reusable parts of an abstract syntax tree.
Fundamental Concepts
The programmer starts with what he desires to have as output of a given set of code. This output is then abstracted as necessary to allow customization based on the input.
Typical abstractions
- field substitution similar to word processing form letters
- extracting reusable sub-templates
- looping over part of a template
- making part of a template conditional
TOP emphasizes what should be produced instead of what the computer should do step by step.
TOP as declarative programming
Imperative programming focuses on telling the computer what to do, whereas declarative programming describes relationships between information.
The drawback of pure declarative languages is that they cannot do anything, since they have no imperative aspect. Thus, most declarative languages have some way to start or otherwise control the execution of the program.
TOP applies declarative programming to code generation. This allows things like referring to information that does “not yet” exist. The processing order is handled by the system. Relieving this burden from the programmer eliminates common bugs, and frees the mind of the programmer to focus on the more valuable, customer driven requirements. This is analogous to lazy evaluation.
Declarative programming solutions have predominantly been used only in academia. They have problems that make them inadequate for most commercial purposes. These issues, however, are mostly related to the runtime use of a declarative system: Difficulty handling the updating of program state, performance issues associated with information updating, etc. Applying declarative techniques at compile time avoids many of these drawbacks.
TOP is similar to generative metaprogramming. Metaprogramming systems generally add abstraction to a single object language. By contrast a generalized TOP system could be applied to any object language.