Probably every programmer in the world has experienced macrocoding. But what exactly is macrocoding? We have macrocoding when, instead of writing some code we need, we write a program that writes that code.

We can see examples of automated code generation anywhere: for instance, every time we run a compiler we have a computer program (for example gcc.exe) writing code (the executable machine code). Macrocoding is a branch of the great family of automated code generation.


The most widely used macrocoding technique, although very rudimentary, is found in the C/C++ programming language. The #define keyword is the foundation of the macrocoding subsystem of the C/C++ language. However, macrocoding can be brought to a much higher level of power by mean of specific tools like Macrocoder.

The video below, courtesy of Dafocus, shows what macrocoding is about:


Why macrocoding?

When we have identified in our project a case where similar coding patterns are to be applied to multiple cases, we can take advantage of macrocoding. Every time we think whenever I have one of these, I have to implement this here and that there, which indeed is a very common though among programmers, we are having a clue that suggests that a macrocoding solution should be investigated. Let's see why.

1 – Faster and shorter coding

The most obvious advantage is that coding becomes faster and much less has to be written. A skilled macrocoder takes little extra time in preparing the macrocoding generator and she or he has just to write the template once. Then it is just a matter of writing only the details that makes the difference of one instance among the others.

2 – Less errors

The second obvious advantage is the reduction of errors:

  1. the output code is generated by the computer that replicates a template without random human errors;
  2. if the error is in the template, it will be replicated on all instances and therefore very likely to be detected very early;
  3. the macrocoding input (i.e. the macro parameters) can indeed contain errors, but being so synthetic, errors are easy to spot.

3 – Macrocode fits your code

Other automation and generalization tools (libraries, frameworks, commercial code generators, etc.), require your code to be written according to their rules. Macrocoding generated code, instead, follows your own rules and fits exactly where it belongs. Macrocoding solutions can be adopted at any stage of the development because the target project does not need to be revolutioned to host them.

4 – Uniform implementation

Since the code is generated according to a template, all cases are implemented identically. This avoids multiple programmers using different implementations for the same concept, causing different behaviours, different weak points and different errors.

5 – Start simple, go complex

Using a macrocoding solution, the generated code can be easily changed by updating the template and generating it again. This can be used not only to add or change features as the project grows, but also to implement the start simple, go complex approach. With this technique the project can start with the simplest implementation: if later on it proves inadeguate, it can be replaced by a more performing one by changing the generation templates. This reduces the number of decisions that have to be taken at the beginning of the project, lessening the risk of carrying on overkilling or undesized solutions.

6 – Store and share knowledge

The macrocoding template stores and replicates the knowledge of whom has developed it. The generated code always contains all the experience and expertise of the template developer, no matter of the level of who is working on the input files. This is particularly important when who knows what the program should do (domain expert) is not an experienced programmer.

Give it a try

Macrocoding generators can be developed using any language, although some are better suited than other. Good results are obtained with scripting languages like Python or PERL, or with dedicated solutions like Macrocoder, that can downloaded for free for non commercial use.