On this fine hump day, let's consider code generation. In this day and age, when we talk about code generation the thing that comes to mind first is typically the kind of code generation that is done with AI. However, there's plenty of sorts of code generation. Long before AI bots were generating code, we've had code generators of various sorts.
One common method of creating a code generator is by using a template. Let's say you have a list of methods for which you want to generate code. You'd then have some sort of program read a template file, then iterate through the list of methods, and generate a function for each one. This method makes it easy to create an interface with all your desired methods, then create an implementation for each method. The only difference would be the template that you use. One template to generate the interface, another template to generate the implementation of each method. The great advantage of doing it this way is that if your implementation changes, you only need to edit the template and then regenerate the file. Another great advantage is that if you want to add another method to your list, you can regenerate your interface and implementation code with the new method, and the first of manually introduced errors is greatly diminished.
Consider also for the moment, AI generated code. There's a level of randomness to AI generated output that you don't get with a deterministic generator. Furthermore, if you are using a cloud AI service like Claude, you might incur costs to generate that code. That means generating the same code using AI can be both less reliable and more expensive at the same time.
Clearly, AI isn't the answer to every problem. Now matter how you love your AI bot and how it can generate code for you, the fact is that it still gives you non-deterministic results. Sure, it's gotten much better, but you'll want deterministic programs for doing things like generating code from templates.
What you can do with AI, however, is ask it to write your code generator for you. Once the bot has written a deterministic method, then you can just run that method to get deterministic results. The AI bot itself is a code generator, and it generates a code generator. Almost like code generation inception.
There are more ways to go about code generation though. Plenty of ways to skin a cat, so to speak. Plenty of ways to eat an elephant, which is best started about the same day as to plant a tree. Something like that.
Sometimes, I have a list of methods and I want to generate code for them only one time. I don't want to create a code generator, a template, all those things just for a one time code generation task. I don't want to hit up AI for such a task either. I'll do what I've done since days long past - write a regular expression!
Some people say, if you have a problem, then you decide to solve the problem with a regular expression, then you have 2 problems. Maybe. There's a reason why regular expressions are so popular though. They work. Not just from inside your program either. Regular expressions can work great as code generators.
Say you have a file with a list of methods. Each method has a list of parameters. You want to generate a file of implementations of those methods. Maybe just stub implementations, or something. Write a regex to capture the method name, then look for the list of parameters in parenthesis. In a copy of your interface file, you'd do a regex search and replace. Why not a regular search and replace? Well with a regex you can do some trickery like look ahead/look behinds. You might have a list of methods, some of which return strings and some of which return numbers. You could write a regex that matches the method definition, but only if the return type (which is typically at the start of the method definition) is an integer. Generate method bodies for each one of those which return a number. Then, do another regex search for methods that return strings. Generate method bodies for each of those which return string values. Just like that, you have stub methods for all your interface methods. No custom code generator, no AI bot needed.
Let's consider another situation in which you might want to generate code from a list of methods. Again, you want to generate code only one time. No need for a code generator program with a template. Maybe you don't care to fire up an AI bot for this task, or you have an exact transformation in mind for your list of methods.
This is where Vim macros really shine. All your editor commands in Vim are done with keystrokes. You might have a list of methods with differing numbers and types of parameters. In Vim, for your transformation, you might copy the list of parameters and generate local variables for each one. Doing so involves a bit of drudgery. You'd copy the type of the parameter to one register, copy the name of the parameter to another register, then you'd paste these two things in the method body from both registers, performing some transformation on the variable name to make it appropriate for being a local variable without hiding the parameter where the variable came from. What I love about Vim is that you can perform the actions you want for your transformation while recording your actions to a macro. Then all you have to do is replay the macro for your transformation. You an even record a macro which will replay another macro. Vim is great that way. You can have searches as part of your macro, and if the search fails, the macro will stop. Combine that with the power of regex transformations too, because Vim is great at regular expressions, even if the syntax is a little different than your run of the mill regular expressions.
There are no comments for this post.
Would you like to leave a comment?