This question asks to create a Clojure macro to generate several functions. We figured out a way to do this but were stuck with the question of “Is this a good idea?”.
My initial reaction is not really, for two reasons
- You then have functions that are not defined in your code, and this can complicate understanding your code quite a bit! (Imagine somebody has a problem with one of your functions and looks at the source code only to not find it anywhere).
- It is better to factor out the commonality of the code in a function or macro. Letting your computer write a bunch of functions that are very alike is a poor approach to that.
What do you think? When does generating functions in a Lisp make sense? Should it ever be ‘on the fly’ or would you prefer to have it in a file somewhere?
TL;DR: It depends.
Can the commonality, or a subset, be abstracted into a function (or functions), with only the more dynamic bits defined through macros? When possible, it’s best to make the macro-y parts as limited in scope as possible.
What’s the nature of the functions/macros? If they’re part of a well-documented system aspect, it doesn’t really matter where they come from.
Are they poorly-understood, and do they require frequent inspection to understand or verify behavior? If so, then leaving them as real functions may make more sense. If they’re not, and are more or less “stock” system aspects, do whatever is cleaner.
Are the functions/macros maintained by everybody, or by someone more focused on an underlying system implementation? If they’re mostly consumed, it matters less how/where/when they’re implemented.