We all use shortcuts to get our job done faster, from copying and pasting code to using snippets or, more recently, Generative AI tools Github copilot and ChatGPT. The downside to these is that they are limited in scope, and they are not flexible enough to handle all the different ways we work with code. In this post, I’ll show you a specific kind of Generative AI tool for code generating that can help you not only to get things done faster but also to help onboard new members to a project, standardize the boring parts and even help you write tests.
What is a code generator?
There isn’t a formal definition of what a code generator is, and if we just think of them as the name implies, it means that it can be anything that generates code automatically, like a compiler.
To clear things up, I’ll narrow down the definition to “a tool that generates code based on a template or context and some user input”.
Personally, I like to classify code generators into three categories:
- Project generators
- Module generators
- Snippet generators
The Project generators are the ones that create a new project from scratch, like create-react-app, yeoman, etc. These are great for getting started with a new project, but you use them once and forget them for the rest of the project.
The Snippet generators are the ones that generate a small piece of code, like a component, a function, etc. Some examples of these are the snippets in your IDE, Github copilot and chatGPT. The limitation of these is that they affect a single file or a small piece of code.
The Module generators are the ones that help you to add features to an existing project, both creating new files and injecting code into existing ones. You can share them with your team, they help you to standardize the code and promote good practices.
These are the ones that I’ll be focusing on in this post, and whenever I mention a code generator from now on, I’ll be referring to this category.
When to use a code generator?
In short, any time that you find yourself doing the same thing over and over. At the beginning of a project, it can be anything like creating a new component or creating a new endpoint in your API (you’ll find a great example at the end).
Later on, when you’ve written a large system, you’ll find out that the majority of the time you spend creating new features is not actually writing the code. In actuality, it’s copying and pasting code from other parts of the system and connecting them to a small subset of modules. This is a great opportunity to use a code generator and automate this process.
What can I use?
This depends on the Tech Stack you are using. If you are lucky, you might be using Laravel or Angular, which already have their own integrated solution.
But for the rest of us, there are a few options:
Both of these are great, but since Plop is heavy on the JS side, I’ll be focusing on Hygen. Hygen is the most “code agnostic” of the two, and to give you an example, you just need to learn the following syntax to start templating with variables:
<%= name %>