Chapter 23. Metaprogramming
Software is like entropy: It is difficult to grasp, weighs nothing, and obeys the Second Law of Thermodynamics; i.e., it always increases.
Norman Augustine
23.0 Introduction
Metaprogramming refers to the ability of a programming language to enable manipulation, generation, and modification of code at runtime. It is fascinating. Once you discover it, you will find it is the tool that solves every problem. But it is not a silver bullet (see Recipe 4.1, “Creating Small Objects”) and is not free, either. Thinking you are creating some kind of magic is the main reason why you should not use it.
Metaprogramming is like design patterns with similar states of excitement:
-
You get to know it.
-
You don’t fully understand it.
-
You study it thoroughly.
-
You master it.
-
You seem to find it almost everywhere.
-
You abuse it (see Recipe 12.5, “Removing Design Pattern Abuses”) thinking it is your brand new silver bullet (see Recipe 4.1, “Creating Small Objects”).
-
You learn to avoid it.
23.1 Removing Metaprogramming Usage
Problem
You use metaprogramming.
Solution
Change the metaprogramming usage, favoring direct solutions.
Discussion
When you use metaprogramming, you speak about the metalanguage and the metamodel. This involves increasing the level of abstraction by speaking above the objects in the problem domain. This extra layer allows you to reason and think about the relationship between the entities of reality in a higher-level language. In ...
Get Clean Code Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.