Generative programming is a programming paradigm in methodical software development. Characteristic of generative programming is the automatic generation of program code by a generator. A program generator can best be understood as an ordinary program based on the EVA principle. Based on certain input parameters, the program generator generates a certain output, the so-called generate. However, the output of a generative program is in turn a program code, namely the code that is to be executed for the specific situation.
How a program generator works
The basis for automatically generated code is the abstraction of frequently occurring program constructs in formal models. Programming is divided into three phases:
- The programming of a specific program generator
- The parameterization or addition and configuration of the formal model to a specific model
- Calling the program generator with the specific input parameters, which then creates the specific target program
A program generator or code generator is therefore specialized in a generic class of applications and programs. It refers to a specific, underlying generic program model, from which it generates the target code after concretizing the parameterization. This can be source code, intermediate code, or binary code.
---
While a normal, functionally programmed program covers the variance of the tasks exclusively with data variables, generative programming also works with variabilized program code, which only becomes unambiguous with regard to the target code.
This approach is particularly suitable for problem solutions that occur in practice in a correspondingly large number of variations, as a considerable amount of effort must be planned for the creation of the model and the generator. This effort can be amortized due to higher quality of the program code and shorter development time. Often, the target programs are only temporarily generated for one-time use and then deleted again. As a result, the program code that persists at a certain point in time, e.g. measured by the number of lines of code, can be reduced by a few powers of ten, if necessary.
Generative programming is useful wherever certain parts of code are to be combined into a large number of target programs in the form of variable text modules. Generative programming also allows the creation of target programs whose target parameters are not yet known at the time of coding the program generator.
Persistent target code
The target code generated by a program code:
- can be created once, stored persistently and then used permanently, or
- can be dynamically created and executed as needed and then deleted again.
If a target code is created once and then persisted, the program generation and the execution of the target program can be decoupled in time. The program generation and the execution of the target program are dependent on each other only insofar as the generation takes place before the execution of the target code. The code generation is then typically initiated by the programmer or by a system administrator during software installation, i.e. typically not by the end user.
For example, a code wizard used to create the base code of a program class queries various parameters, such as class names, number, names and types of class properties, number and names of class methods, and then creates the program code of the class.
A new generation is only necessary if there are changes to the generation parameters.

Dynamically generated target code
In the second case, the program generation and execution of the target code is usually initiated directly by the end user. Ideally, the program generation is so fast that the end user does not even notice that the part of the program he is using has only been automatically programmed a few fractions of a second ago. The sequence of this dynamic process should be traced in individual steps:
- The user makes a selection of the input parameters, e.g. the name of a database table.
- The program builder takes the table name from the end user, reads the fields, field types, and foreign key relationships from the database’s data dictionary, and uses these control parameters to generate the program target code of a search form for displaying data for the database table specified by the user.
- The program target code is now compiled briefly and then executed by the program builder with a dynamic call.
The last step places certain requirements on the programming language used:
- It must be possible in the program generator to call a routine whose name is variable and not necessarily known in the context of the program generator (e.g. default of the routine to be called by a string variable, late binding).
- The necessary flexibility in program generation requires an interpreted language, i.e. as a rule, an interpreter code is generated as the target code and not a machine code. Basically, the target code can be anything, i.e. source code, intermediate code, or binary code.
Sensibly, the dynamically generated target code is often coded in the same language as the code-generating program module. A program generator is a code generator that specializes in a generic class of applications and programs. It refers to a specific, underlying generic program model, from which it generates the target code after concretizing the parameterization. Complex parameterization can be carried out, for example, via a tabular model in a database, which controls flexible code generation.
Also read:
- Application Examples of Generative Programming
- What is Large Language Model (LLM)
- AI Generated Art and Generative Art