Home 9 Daniel Zacharias 9 Metaprogramming: What It Is and How It Works

Metaprogramming: What It Is and How It Works

This technique builds programs that treat code as data. Therefore, the program can create, read, analyze, generate, manipulate or use other programs. It may sound a bit convoluted, so let’s break it down.
Daniel Zacharias

Daniel Zacharias

September 12, 2022

There’s something about the future that makes us dream about smart gadgets and gizmos, able to do their job without any human help. Autonomy and efficiency are synonymous with the future and progress. When programming, there is something that, at first glance, could fall into this category of self-controlled and independent tools: metaprogramming. It’s a buzzword that’s been making the rounds lately. 

But what is metaprogramming, and how does it work? 

What can we do with this powerful tool, and what challenges does it bring to the industry?

What is metaprogramming?

Imagine that you start making cars from scratch. Slowly, your cars become famous, and more people want to buy one. There is one problem though — you are just one person, and there are lots of clients. Fortunately, the process is routine and can be modeled with an algorithm. 

So, you build a machine to make those cars. As your car-making factory grows, so does demand, but now you have a new problem, you can only make so many car-making machines. 

Once again, you make a machine, this time one capable of building machines that make cars. 

Change machines for programs and cars for code, and you’ll have a pretty good idea of what metaprogramming is.

In a nutshell, metaprogramming is writing code that writes code, but if we are looking for a better definition, let’s expand that simple notion. This technique builds programs that treat code as data. Therefore, the program can create, read, analyze, generate, manipulate or use other programs. 

It may sound a bit convoluted, so let’s break it down.

A metaprogram is a program that can:

– Accept other programs as its data

– Manipulate said data, inspecting and modifying the behavior of the program

– Inject new behavior in the program if necessary

– Make Monkey patching: tweaking parts of the program at runtime without changing the source code

How to use metaprogramming

Let’s get one thing out of the way: metaprogramming is nothing new. Even a junior developer can write simple metaprograms with minimal effort. So why is the term making the rounds? Because machine learning is giving us the tools to build very advanced metaprograms.

Metaprogramming is used daily by software developers all around the world but it’s so subtle that it’s barely noticeable. 

Example 1

Take, for example, IDEs like Visual Studio Code or Pycharm. Aside from helping us organize our code, these programs do a bit of metaprogramming.

Both platforms flag potential errors without having to run the code. Both platforms offer basic level refactoring, translating a block of code into a more readable syntax. Pretty useful, but nothing groundbreaking.

Example 2

What if we had a program that could write code with very little input from the developer? Well, that’s the kind of service GitHub Copilot, Intellicode, ReSharper, and other coding companions offer for modern software developers.

Most of these systems are based on machine learning. Using billions of lines of code from repositories like GitHub, artificial intelligence is trained to detect and understand code patterns that it can later reproduce to write new code.

Some of these tools are so extraordinary that a developer only needs to write a comment about what the code needs to do, and the assistant will return a code block ready to be run. 

Talk about machines stealing our jobs.

Example 3

Another great application in metaprogramming is what we call reflection, in which software is its own metaprogram. In other words, the code takes itself as data and rewrites itself according to its needs.

For example, imagine having a JavaScript program that creates an object called “Cake”. Within the object, we might have a function called “self_edit” that triggers during runtime. Before Cake is manipulated, it checks via the self-edit method if its data structure is adequate for whatever operation is about to happen. If it’s inadequate, then the self-edit method restructures Cake before passing, preventing errors.

In other words, we have a program capable of fixing itself. You can have metaprograms for pretty much anything, from adapting software depending on workloads to changing infrastructure as the need arises. It’s an amazing technique with infinite potential, especially when we pair it with the learning potential of AI.

Metaprogramming dos and don’ts

DO use AI assistants for time-consuming tasks

A lot of the code we write is time-consuming but doesn’t require a lot of thought. Thanks to AI assistants, we can reduce the time we spend writing simple code and focus instead on more complex problems.

A great example I always like to bring up is using AI assistants to learn about new technologies. If I wanted to learn a new language, like Rust, I would have to go over the documentation and probably spend a while looking at videos to learn the basics.

But what if I could ask an AI to write a program in Rust and then use that as a learning tool? By studying how the assistant wrote a function, I can have a pretty good idea of the underlying syntax — in other words, learning by doing.

DO NOT trust AI assistants blindly

On the other hand, AI assistants are far from perfect. Since they are trained with open-source code, the resulting code may be functional but buggy, convoluted, or pose a security risk.

Artificial Intelligence is only as good as the data used to train it. If a developer community has adopted bad practices and we feed that code to the AI, the output will have those same bad practices. It’s up to us to review it and ensure that the code meets our standards.

DO write a best practices manual

When doing consulting, I’m always amazed at how often people forget the importance of building a set of good practices and training their teams to follow them. 

Metaprogramming is like any other technology, and it can boost our project significantly if we know how to apply it. 

Think about what you want from a metaprogram and teach your team about its potential and issues.

In summary

AI-powered metaprogramming has the potential to revolutionize the industry as we know it, but only if we have the wisdom to understand its limits and its potential downfalls. 

Metaprograms are not a replacement for humans but rather powerful companions that can help us transcend our limits.

Get the best of Code Power News in your inbox every week

    You may also like

    Principles and Benefits of Reactive Programming

    Principles and Benefits of Reactive Programming

    Unlike traditional programming, reactive programming revolves around asynchronous data streams. However, code is usually written linearly, one step after another. Reactive programming offers a way for developers to deal with scenarios where events occur unpredictably...

    Get the best of Code Power News in your inbox every week