Source-Code Generation
Source-code generation and generative programming in general is the idea that:
“programs can be written to manufacture software components in an automated way just as automation has improved production of traditional commodities such as garments, automobiles, chemicals, and electronics. The goal is to improve programmer productivity.”
– Extract from Automatic Programming on Wikipedia.
A Bit of History
Back when I was learning how to program, I thought source-code generation was generally seen as a bad practice. To be clear, I am not talking about a feature-complete programming language like TypeScript that compiles into another language, but simpler features similar to the Entity Designer from VB.net.
ADO.NET Entity Data Model Designer
The Entity Designer was a visual tool that allowed you to create tables and its relations. The table class would then be generated automatically and could be used in your code.
This saved you a lot of time which would otherwise be spent creating many somewhat similar classes to handle all your entities.
The downside of this tool was that it was hard to edit or add custom code to the generated classes. You would often want to do so because such tools hardly ever let you do everything you wanted to. Sure, you could extend the class, but even that had its limitations. You could never directly edit the generated classes because the idea was that you could go back to the visual editor and change things; that would override the changes you would have made if the editor let you make them in the first place. You can read more about this here.
Of course, we are talking about a well-funded technology developed by Microsoft here, anything of a smaller scale would likely have more disadvantages.
Ruby on Rails Scaffolding
A few years later, I started experimenting with open source MVC frameworks. First with cakePHP, then with the one that inspired it: Ruby on Rails. These introduced me to an entirely different sort of code generation using command line generators.
I really loved Rails, and the way it taught you to create your first model using scaffold was simply brilliant. With a simple command, you would get everything you would need to have minimal models working: from a migration script that could create the table in the database to the CRUD view that contained basic HTML pages, complete with a controller, a model, basic unit tests, etc.
The key difference here is that the generated code is meant to be edited, it only generates very basic files that you can keep working on. You are not meant to run the command more than once per model but if you used change merging tools like the ones we use for Git, even that could be possible. The result is that you have full control over your code. Read more about Rails here.
The Advantages of Generators
Saving Time
This is the most obvious advantage, if you write something often, making something write it for you will save you time. It also cuts down the time you’d invest in memorizing the details of your routine tasks.
Improving Code Consistency
A side effect of generating your code is that by using the same template each time, your code will look more consistent. This is particularly true for generators that will create a tree of folders or files, making it easier to find your way through several projects.
Some Pitfalls to Avoid
Don’t Trust Them Blindly
When you are using a generator, don’t assume the generator is always right and don’t be scared of changing anything it generates. Usually a generator will generate things that are most widely used, but it will not always fit your needs. A good generator should not only give you a good base, but the result should be easy to modify, keep this in mind when looking for a generator you want to use.
Creating One Yourself Can Take Too Much Time
Creating your own generator can easily end up taking more time writing than manually writing the code yourself, even if you need to perform a few repetitive tasks. The first thing you should do is check if a relevant generator already exists, and if it’s not perfect, editing it may be faster than creating a new one. If you do think you should make one, make sure the generator you make is pretty simple and can be used in various contexts. If you are not sure how much time you should devote to work on one, this should be a good point of reference:
Generators Can Be Complex
The code of the generator will be, by nature, more complex than the code they generate. Keep this in mind when maintaining your own system because the complexity might make it less accessible to potential contributors. To limit this, a generator should not try to cover all use cases, rather it should let the programmers that use it fill in the blanks themselves.
Generators in Our Everyday Work
Today, this idea of a generator that would lay a simple ground work as a starting point is quite widespread.
NPM Init
“Init” commands are everywhere, whether it’s starting a new project or installing your favorite framework, there is often a command that will create the preliminary files you need.
NPM is a good example of this, you don’t need to remember all the syntax of the package.json file to start a new project. Simply type “npm init”, answer a few questions, and just like that you are ready to go.
Your IDE is Full of Generators
The goal of an IDE is to help the programmer work faster, and one of the ways it does this is by generating bits of code for you at key moments. There are also plugins within IDEs that can further improve code generation.
When your IDE detects an error, it sometimes proposes ways to resolve the problem automatically, and this will often trigger some code generation. A few examples include adding the missing “require” for a module or class automatically, creating the skeleton of a missing method or property in a class, adding “try catch” automatically around a method known to throw errors, etc.
When creating a new file, some IDEs will suggest a template as a starting point for that type of file. It will then often use the name of the file to auto-fill the name of the class, the docstring, or other relevant information.
Some IDEs have commands and key shortcuts to generate code while you work. For example, you can sometimes generate a comment block from the name and type hinting of a function.
Great Tools to Supplement Your Workflow
Yeoman
Yeoman is a tool to make generators whose team also maintains a huge database of pre-existing generators.
If you start a project with a specific combination of frameworks and/or libraries, you could easily search through their database to see if there is a generation that will create the basic setup for you. If you are lucky, you may even find one that will help you with common tasks long after the project has started.
Yeoman works on Node.js, but it is possible to have generators for any language.
A notable example is a generation that creates a base for a VS Code extension. It is maintained by Microsoft and featured in their documentation.
The Symfony MakerBundle
This package is essentially the Symfony version of the generations that Rails uses.
Most of the popular components have generators. These provide a good base, and you can even install others, personalize existing ones by copying them, or create entirely new ones. And yes, there is a generator to create a generator (the base of a generator). Most of the tools listed here offer such a thing.
Emmet
Emmet is a bit different, it is not a command line tool but a plugin for many popular text editors and IDEs. It even comes pre-installed in many editors.
Emmet allows you to extend small abbreviations to full HTML or CSS code. For example, simply type “.content”, do a simple keyboard shortcut, and you get ‘<div class=”content”></div>’.
Try the demo, use abbreviations like “ul>li.item$*5” and you will never want to write vanilla HTML ever again.
Cookiecutter
Cookiecutter is the equivalent of Yeoman for Python lovers. Once again it is not strictly limited to Python code, but that’s the language it runs on so you are much more likely to find generators for Python libraries.
Hygen
Hygen is a lesser-known alternative to Yeoman, but nevertheless, it makes creating new generators much simpler.
Here, you don’t have a massive database of generators, but if you want to create your generator without much head scratching, you could give it a go.
My main reason to include it on this list is because it’s creator wrote an article that inspired, in part, this article. You may give it a read here.
CodeWave
In my spare time, I sometimes work on a little thing called CodeWave. It is inspired by Emmet but meant to be used for backend technologies and offers more dynamic templates. It is still a work in progress, but if you want to give it a try, you can take a look here.
Final Thoughts
This article is a simple and quick overview of source-code generation and generative programming, primarily focused on how it relates to the tech stack at SSENSE. There are many similar tools available out there and I hope you will go ahead and try some. If you are looking for further readings on this subject, here is another article that talks about some of the things I skipped over.
Editorial reviews Deanna Chow, Liela Touré, & Prateek Sanyal.
Want to work with us? Click here to see all open positions at SSENSE!