Code generation is the process of converting some format of input into code. The input can be configuration file, or code. Code generation has many use cases including generating:
- Domain specific languages (Excel)
- Types from meta information to write safer code (https://github.com/adelsz/pgtyped or https://github.com/SweetIQ/schemats)
- Clients from API meta information (https://github.com/acacode/swagger-typescript-api)
- Compilers from grammar (https://github.com/tree-sitter/tree-sitter)
- Boiler plate code like getters and setters in Java or scaffold in rails (https://www.rubyguides.com/2020/03/rails-scaffolding/)
- Documentation from code (https://jsdoc.app/about-getting-started.html)
P.S. In this article we are referring to DSL kind code generators and not compiler's code generation phase.
The following things should be kept in mind when writing code generators:
- Is the generated code expected to be read?
- If yes, (e.g. rails scaffold) then it should follow human conventions and abstractions, DRY principles etc
- If no, then it can print repeated code for performance
- Is the generated code expected to be changed?
- Two possibilities
- The generated code is expected to be changed by the user, let's call this "user-land" (or scaffolding)
- Example, concrete typed functions that can contain business logic-
- This is usually done once in a project's life cycle for a module and after a user changes the files, it is not possible to generate them again
- The generated code is not-expected to be changed by the user, let's call this "library-land"
- Example, types (models) for tables in the database
- This is usually done multiple times in a project lifecycle, say when there are new tables/changes to a table in the database
- Is the generated code expected to be versioned?
- This is usually a team preference. Most teams version their generated code.
- But if you want to treat your generated code as a perfect abstraction (which is tested elsewhere), you may not want to version it
- https://tomassetti.me/code-generation/ - This blog is by far the best resource on Language Engineering topic and this article is a deep dive into this topic
- Book: Language Implementation Patterns