There is different types of code generation, mainly because of the inflationary usage of the word code. It ranges from QR code, some pass code like secret keys, machine code generation, generation of secret languages. When I write about code generation, I want to talk about source code generation. Generating source code and avoid to write and maintain it yourself.
When I think back, I think it was in the very beginning, when making websites with PHP, that I was doing code generation for the first time. from PHP I was rendering data directly as JSON into the embedded JS source code.
Very popular are so called scaffolding tools. Within node I believe the most popular is yeoman. With its many generators. This kind of tools is usually taking some template and user input and generate a first boilerplate project, to get productive quickly. Many scaffolding tools then provide further commands for generating typical components, such as database models or UI components. it is cool when the authors of such tools support generation as well as removal of modules. Today there are basically scaffolding tools for every major framework or library. The main point that classifies these type of tools, is that everything that get generated comes from the tool-authors and the input of the user.
Some of these tools get embedded directly into the IDE’s, like grating getter and setter functions. and many IDE’s can be extended with more sofisticated generation and refactoring tools via plugins.
Recently I had do work on a project that is using the graphql-code-generator. For using this tool, you write the graphql schema and it can generate typescript definitions and if you want even definitions for mongoose. Using extra plugins you can even more code, such as for your front-end or what ever other systems are going to work with.
Long before, when I was developing my tmongodao library, I also thought about generating type definitions. as the support for typescript in that lib is not very deep.
In a current project I see many files maintained, that reflect the models and schema in different forms, having the object keys camelcase or snake case. It made me also want to generate that definitions automatically. but instead of the graphql api definition or the schema of an ORM, I wanted to use the database as source of truth.
Talking with a colleague about this Idea, he told me, he used such a technology before within a C# project. With a little bit of googling, I found a small project I liked. schemats can read the schema from mysql or postgresql and generate typescript definitions.
You see, that this kind of tool is taking some source of truth. Whether it is the API definition, the database schema from code or the database itself or a third description language, the tools can read some information and avoid writing code and minimize the maintenance.
When once starting to auto generate code, you can get into the habit and even get caught up into automating all the way. Doing a kind of meta programming.
This is when processing pipelines come into play. Gulp or Grunt have been the most popular within the Node.js ecosystem. With extensive plugins and configurations, you can apply many code processing and generating tools.
Personally, these tools have always been one step to much. In order to? configure the tool right with all its plugin, you can have a project finished by hand before.
So this is my take on code generation today. Please to not get caught up in automating everything, get stuff done. But use tools, to progress in your projects. I guess soon you will read about overtooling on this blog.