Is source code generation an anti-pattern

anti-patternscode generation

If something can be generated, then that thing is data, not code.

Given that, isn't this whole idea of source code generation a misunderstanding? That is, if there is a code generator for something, then why not make that something a proper function which can receive the required parameters and do the right action that the "would generated" code would have done?

If it is being done for performance reasons, then that sounds like a shortcoming of the compiler.

If it is being done to bridge two languages, then that sounds like a lack of interface library.

Am I missing something here?

I know that code is data as well. What I don't understand is, why generate source code? Why not make it into a function which can accept parameters and act on them?

Best Answer

Is source code generation an anti pattern?

Technically, if we generate code, it is not source even if it is text that is readable by humans. Source Code is original code, generated by a human or other true intelligence, not mechanically translated and not immediately reproducible from (true) source (directly or indirectly).

If something can be generated, than that thing is data, not code.

I would say everything is data anyway. Even source code. Especially source code! Source code is just data in a language designed to accomplish programming tasks. This data is to be translated, interpreted, compiled, generated as needed into other forms — of data — some of which happen to be executable.

The processor executes instructions out of memory. The same memory that is used for data. Before the processor executes instructions, the program is loaded into memory as data.

So, everything is data, even code.

Given that [generated code is data], isn't this whole idea of code generation a misunderstanding?

It is perfectly fine to have multiple steps in compilation, one of which can be intermediate code generation as text.

That is, if there is a code generator for something, then why not make that something a proper function which can receive the required parameters and do the right action that the "would generated" code would have done?

That's one way, but there are others.


The output of code generation is text, which is something designed to be used by a human.

Not all text forms are intended for human consumption. In particular, generated code (as text) is typically intended for compiler consumption not human consumption.


Source code is considered the original: the master — what we edit & develop; what we archive using source code control.  Generated code, even when human-readable text, is typically regenerated from the original source code.  Generated code, generally speaking, doesn't have to be under source control since it is regenerated during build.

Related Topic