🎄You should learn to write and use Code generators in 2024🌟

Dec 7, 2023

Back to 2023 Advent Calendar

What are the codegen tools?

Codegen tools are tools that generate code for you. For example, GraphQL codegen is a tool that generates typescript (and other language) types for your GraphQL queries. Similarly, there are many other codegen tools for different use cases.

Why Software Engineers need to know how to generate code?

Codegen tools are very useful for software engineers because they can save a lot of time and effort. Some codegens already exist and ready to use like GraphQL, Protobuf/grpc, Thrift, NextJS starter etc. But sometimes you need to write your own codegen tools for your specific use cases. For example, you might want to generate a lot of boilerplate code for your project.

Which tools should I use to write custom codegen tools?

There are many tools available for writing codegen tools.

There are usually divided into 2 groups:

  • Template based
  • AST based

In template based codegen tools, you write templates and then you pass data to those templates and it generates code for you. For example:

Example of template based codegen code:

const javaClassTemplate = `
public class {{className}} {
    public static void main(String[] args) {
        System.out.println("{{message}}");
    }
}`;

// Compile the template with Handlebars
const template = Handlebars.compile(javaClassTemplate);

// Data to be inserted into the template
const data = {
    className: 'HelloWorld',
    message: 'Hello, World!'
};

// Generate the final Java class
const javaClass = template(data);

The downside of template based codegen tools is that they are not very flexible, and don't necessary produce valid code (e.g missing imports, semicolons, etc)

AST based codegen tools

AST based codegen tools are more flexible and produce valid code. When you go with AST approach you need to construct AST (Abstract Syntax Tree) of your code and then you can manipulate it and generate files from it.

For example, here is how you'd create a method in Java using JavaPoet

MethodSpec main = MethodSpec.methodBuilder("main")
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                .returns(void.class)
                .addParameter(String[].class, "args")
                .addStatement("$T.out.println($S)", System.class, "Hello, World!")
                .build();

then you can add this 'method' to a class and generate a file from it.

What is the problem with AST based codegen tools?

The problem with AST based codegen tools is that they are very hard to write. You need to know a lot about the language you are generating code for.

In conclusion, I just want to say that codegen is a game changer for software engineers, once you know how to use it opens up a lot of possibilities for you.