Code Generators

The last section of the book is about code generators. This is indeed something we need to know as programmers since it will improve out work. Most likely, you are using code generators but maybe didn’t know. For example, if you are using some ORM you might be using them, or maybe you generate code for API calls. It happens that I wrote such a tool for Unity3D just a couple of months ago. It’s a pre-production library but I can see its potential. You can check it out under project, SjwagMeister.

But generally, code generators are used to remove the repetitive tasks or to help you write cumbersome code. So it saves us time, and allows us to focus on the hard parts of the code.

Andrew and David separate two types of code generators: passive and active code generators were the former is generally used only once, like generating code from UML for example and the latter generator is ran every single time in the build pipeline of your game.

Let’s zoom in a bit on passive code generators.


Passive Code Generators

Passive code generators save you typing as they are parametrized templates to generate output. Once the generation process is complete, they produce full fledged source files which you can add to your project. These can be added to your project, which you can then edit and add to source code control.

So passive code generators have many uses and among them are that, well you can use them to create new source files. But you can also use them to insert all these annoying legal statements in your source files. Imagine what life would be like if you could not set certain templates in your IDE or in your Unity3D editor. You would have to add all that cumbersome junk all the time. Even typing “public class Foo : MonoBehaviour { protected override Awake(){ blablabla…” that would suck big time. So thank god that we can set these templates.

Andrew and David also mention that you can use code generators to translate code from on language to another. I’m not so sure this happens a lot in Unity3D land, but I do know that in Java land this is used a lot. It’s even a feature in Intellij to automatically translate Java to Kotlin. You will end up with Java-like Kotlin and it will work and compile, but it wont be idiomatic Kotlin. Which is perfectly fine since you can then change it while the tests are still running and compiling!

The example they give in the book is that they changed “code” from one templating language to another. For this book even, they started working in a system called troff, but after 13 sections they decided they wanted to continue in LaTeX so they wrote a little converter to move from troff templates to LaTeX templates. This makes sense indeed; Maybe in a unity context, you could convert HTML and css to UXML and Style files that UIToolkit accepts. That sounds pretty neat btw, and is probably already done somewhere.

Another example of passive code generators is to use them for precomputing lookup tables for things that are expense to calculate at runtime. I think I’ve seen this in some matrix multiplication algorithms once. In the CS50 course given at Harvard university, I’m not sure though.


Active Code Generators

So the other type of code generator they talk about is what they call active code generators; these are the kind of generators that generate code when as soon as other things change. Think about what these ORM’s are doing. If you have ever worked with Entity Framework, you know exactly what I mean. Once you change the schema, your code to access them changes as well, maybe it doesn’t even compile anymore. This is a very annoying yet useful attribute haha.

They also specifically mention the fact that active code generators are often used to bridge the interop layer between two programming languages. If one language needs to communicate with another, you might want to generate the API to do so automatically. This again comes back to ORM’s; they generate an API to communicate between C# and SQL.


Code Generators Needn’t Be Complex.

Andrew and David also mention the fact that code generators sound complex, but they don’t have to. They simply output text based on some input. It’s often the parsing part that’s most complex, not the generation part. Let me give you an example of a code generator I wrote recently.

A while ago, I got frustrated by needing to implement yet another front-end to some back-end API. I needed to essentially translate the DTO or Schema files from the back-end to the front-end, and then create some logic to send HTTP requests over the wire. I decided this was the last time I would do that, and thus in my personal time I started working on a Unity3D compatible OpenAPI generator. So it takes the OpenAPI spec, or Swagger docs as some might call them, and generate all the DTO files and even the web-requests to call them. I’ve got an alpha version working with limited functionality but it thought me a lot about the different forms of code generation in .Net. It’s a little project called SjwagMeister.

In my research on how to properly generate code for Unity3D projects I found there were essentially three different solutions; the first one is the classical T4-Template approach. This is an approach based on text-templates which you can convert into any kind of other text you want. You can generate pure .txt files, but also JSON, YAML, javascript or C#. Really, any kind of plain text will suffice. The second approach is more modern and it’s called .Net source code generators. These are also accepted in Unity3D and they use the Roslyn c# compiler to do it. You can compile code at runtime using this. I didn’t choose source code generators as I really wanted to generate actual files that I can check into source control. This way I can also properly version things. The third approach is to simply roll your own, which I also did because T4-Templates are not supported on .Net Standard api compatibility level and I really wanted to support both.


Code Generators Needn’t Generate Code

And the very last subject of this chapter is that, well, code generators don’t actually need to generate code. This might sound counterintuitive, but code generators can of course also generate JSON, YAML, XML or some other kind of markup or plain text format. Heck I’m currently working on some really cool UI generation logic at work; but I can’t really talk about it just yet.

But think about all this Swagger documentation out on the web, or maybe even the doxygen or javadocs that are generated based on source code files. These all count as “code generators”.

If you have never used or written any kind of code generator, look into them. They can really ease up your way of working and take over the cumbersome tasks for you. You only have to write the generator once! Remember that.



01010010 01110101 01100010 01100101 01101110

Hey, sorry to bother you but you can subscribe to my blog here.

Never miss a blog post!

You have Successfully Subscribed!