How to warn new users away from metaprogramming

I feel fortunate to have recognized early on that metaprogramming was generally above my skill set! Based on the number of questions that get posed to the forum about it, I think it has saved me some headaches.

But I definitely appreciate the efforts of those who have ventured into the pits…

The biggest problems I see with mis-use of metaprogramming are

  • people who fundamentally don’t understand what macros are and when they run, and are trying to use them for runtime calculations or think they can do something other than saving typing. Consequence: frustration and confusion.

  • people who want to pass function arguments as strings or expressions rather than as…functions. Consequence: frustration and confusion about scope, and terrible performance if they get it to work.

  • trying to use lots of generated variables rather than using a container data structure (e.g. a Dict). Consequence: frustration and confusion about scope, and (probably) inflexible and unreadable code.

24 Likes

Not to derail the conversation, but: One could argue that MathOptInterface is the macro-free interface, and JuMP is “just nicer syntax”.

1 Like

On the other side macro are fun and an exciting feature when you come from other languages that don’t have them. That’s something that motivated me to go into Julia early on, and even though I made all the common mistakes with macro it was still fun (and useful) to learn. That’s also maybe why we see all these beginners misusing macros.

5 Likes

Just to clarify, the proposal is about the scenario where macros yield difficult, buggy, inelegant, and slow solutions. It is my impression from discussions on this forum that the new users who get into these are not having fun — quite the opposite.

There is no intention to deny the usefulness of macros, or to de-emphasize their importance in Julia and how they give a comparative advantage to the language.

Also, from this discussion I think that the title of the topic is too broad: other aspects of metaprogramming are not as affected as macros.

4 Likes

It would be a good start. And if documentation can be divided into “Basic”, “Advanced” section and put metaprogramming under the “Advanced” section would help shun away new users from trying it.

Current TOC may make one think that metaprogramming is as easy and essential as Multi-dimensional Arrays and Missing Values

6 Likes

Absolutely. MathOptInterface is the macro-free interface under JuMP.

Do folks think this pr adequately gathers the sense of this thread?

2 Likes