- Template Oriented Programming
In
computer programming , Template oriented programming (TOP) is aprogramming paradigm that focuses ontemplates to accomplish a programmer’s goals. Template oriented programming is a more general version ofgeneric programming in which the template is the fundamental abstraction mechanism, as opposed to traditional generic programming applications that add templates to traditional languages such asC++ , 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 processor s 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 anabstract syntax tree .Fundamental Concepts
Similar to the paradigm shift that occurred when moving from structured programming to object oriented programming, or from unmanaged to managed, garbage collected code, programming in the TOP paradigm requires a different way of thinking about the information. 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. The abstractions include: Simple field substitution similar to word processing form letters, extracting parts of the template into sub-templates that can be reused, looping over part of a template, making part of a template conditional, etc. The paradigm shift requires thinking about what should be produced instead of thinking about what the computer should do step by step.
This discussion leads to a short rabbit trail.
Imperative programming languages (Fortran ,BASIC , Java and C for example), evolved frommachine language , and focus on telling the computer what to do.Declarative programming languages (Lisp,Prolog ,XML ), on the other hand, describe 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 the technique of declarative programming to code generation. This allows things like referring to information that does “not yet” exist. Such references, when first evaluated, will be null. However, when the information is “later” created, the reference will be created correctly. Notice the time related words were placed in quotes. That is because, in TOP one does not think about time. The processing order is handled by the system. Relieving this burden from the programmer eliminates many common bugs, and frees the mind of the programmer to focus on the more valuable, customer driven requirements.
A simple example is in order at this point. Let’s say a program was to evaluate a parenthesized mathematical expression. Assume the expression is mapped to a tree, with the leaves being the inner most expressions. A traditional imperative evaluator must find the leaves, evaluate these first, and then work up. A declarative solution, on the other hand, can simply state the desired result for each operator. All sub expressions are evaluated independently, and parent expression results will eventually be updated whenever a sub expression result changes. The system is responsible for making sure the result at the top is correct based on the last changes to any sub-expressions.
Declarative programming solutions have predominantly been used only in academia. They have many 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. The TOP paradigm, instead, uses declarative techniques at compile time, avoiding many of these drawbacks.
This field is similar to that of template meta-programming. Template meta-programming systems are generally applied to a single, existing language. C++ templates, for example, allow adding templates to the C++ language. The focus is how to add compile time abstraction to the C++ language. TOP, on the other hand focuses on the templates, with the output language (or languages) being a secondary concern. In addition, the target language is defined by the programmer.
Wikimedia Foundation. 2010.