Code generation with Sed

Writing repetitive code is boring, and not doing so should be a goal for programmers. After all, what’s the point of being able to program a machine to execute repetitive instructions if we’re going to do the same thing ourselves.

Unfortunately, macros or build-time code generation isn’t always an option. Depending on the project, such approaches might not be suitable because of added complexity or because they deviate from the rest of the project.

In those cases; we can use shell scripts to generate code, and then commit those files into the codebase. This approach is similar to what a lot of frameworks (like Rails and aspnetcore) already do. In most places this is called scaffolding.

As an example, let’s make a script that generates Models for our API endpoints. Let’s get the name from the command line arguments.


Comments BETA

Page built on Sun Apr 7 22:36:43 BST 2019