6

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

  1. 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).
  2. 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?

5 Answers 5

4

The complaint about code complexity have been following macros around for years. Every abstraction is there to hide complexity, whether it's a macro, or function, or whatever.

The value of factoring to a function is reuse, as functions are more reusable than macros. Not just in the case of being able to use "apply", but in the literal case of shared code. A shared function is simply a pointer to implementation of the function. A shared and reused macro results in several COPIES of functions or code or whatever, and while the abstraction is there, the code is not shared at all within the system.

Now, you could make a really clever macro that checks for a function definition at expansion, and if it doesn't find it, then it can create the function on the fly, or do some other clever thing.

But even with the functions factored out, they're still going to be ostensibly hidden from the user since that the basic premise behind the macro in the first place. Putting those helper functions in to some hidden package doesn't make then any more visible to the consumer unless they know to even look within the source code (assuming they even have the source code).

Ideally, the functions would be uninteresting to the developer, as they have "no user serviceable parts inside". If they do, then the macro is not sufficient support or documentation for these function in the first place.

6
  • 1
    so...are you saying "it depends"?
    – Kevin
    Oct 21, 2011 at 19:15
  • Concerning code complexity / understandability: The assumption was that other people (developers on the same team, open source library consumers, etc) may need to understand your code. E.g. for me it is a huge benefit that I can look up how most things work in clojure.core. If a function isn't defined in a file because it is generated at macro expansion time by a macro, that becomes more difficult.
    – Paul
    Oct 21, 2011 at 19:17
  • @Paul, but it's defined in the macro the file used (and you can always manually expand the macro). What's the difference? How is it any different than referring to any external artifact not local to the file? Oct 21, 2011 at 22:18
  • @WillHartung If you look for it in the source you won't find it and will possibly have to dig through every different macro to see where it is generated.
    – Paul
    Oct 21, 2011 at 22:50
  • @Paul why wouldn't you look in the source of the macro, since that's the code that generated it. Or look at the expansion to see what was generated. Or why wouldn't you curse the developer for doing something that makes you have to scour the source code for this information in the first place? Oct 21, 2011 at 23:25
3

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.

2

Good tools will take care of concern #1. In Emacs/SLIME, just press M-. on a symbol and it takes you to wherever it was defined. Of course, if your macro is very complicated that may not help you much but that's another concern.

As for #2, that's very much a design consideration. You can write functions "on the fly" using no macros at all and it won't make much difference.

My personal main concern re macros, especially macros defining vars, is that they turn out to be less composable than I want them to be. I think I agree with Dave Newton here that you should limit the scope of what any particular macro does and try to factor out as much code into public functions (and maybe a few simple macros) as possible. Especially if your macros create some object and assign it to a var (i.e. anything that starts with def...) you probably should make sure there is also a way to create that object "anonymously" and if you don't need a macro for that, even better.

3
  • 1
    I don't believe #1 is true (I think emacs would not find a function that was created inside a macro). Totally agree with the composability problem of macros. Everything that can be done in a function should be done in a function.
    – Paul
    Oct 22, 2011 at 10:49
  • Try it out. It definitely does work in SLIME. It works because slime just asks the clojure runtime where the var was created, and that meta data is automatically added whenever a var is def'd. Oct 22, 2011 at 10:53
  • Ok, I'll take it back then. Very nice that it works. I guess it is a lesser concern then.
    – Paul
    Oct 22, 2011 at 10:55
1

Fundamentally, if you can do more with less thinking - presuming your abstractions are clean, don't require tweaking constantly and well named - it is a good solution.

I am a big proponent of using the best feature in the best place, and using everything in your power to write less code to do more.

0

I've found that you can get back some readability when using this approach, by thinking of the macro-generating-lambda routine as a code generator, and then defining functions that take advantage of that generator, but are defined explicitly (using defun).

For example:

(defmacro code-generator (&body body)
  `(lambda (x y)
     ;do some stuff that's duplicated by a lot of functions you want to write
     ,@body))

(defun fun-a (x y)
  (funcall (code-generator
             (do-this on-some-var-in-code-generator-environment))
           x y))

(defun fun-b (x y)
  (funcall (code-generator
             (do-that on-some-var-in-code-generator-environment))
           x y))

(let ((closure-val 'some-val))
  (defun fun-c (x y)
    (funcall (code-generator
               (combine closure-val with-some-var-in-code-generator-env-etc))
             x y)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.