♣️
Thoughts on Code Generation

Introduction

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)
P.S. In this article we are referring to DSL kind code generators and not compiler's code generation phase.

Writing Code Generators

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

Resources

  • Book: Language Implementation Patterns