Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

How often do you or have you seen meta-programming be used in projects? In uni I've never seen this be applied, but I've seen this on my previous job (when I saw it I was blown away of how effective it was).

But how common is it? Is it used all the time, or just occasionally?

share|improve this question

closed as too broad by MichaelT, Michael Durrant, World Engineer Nov 12 '13 at 23:15

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
It might not be that common; though quite common in Lisp, Lisp is not that common -- not even Common Lisp. –  paul Oct 10 '12 at 16:03
    
explicit or implicit? At one extreme using Java and C# is arguably meta-programming since their compilers output language files that are then compiled or interpreted. At the other just about no-one writes code to generate code in a language that they have built a compiler for. –  Ӎσᶎ Nov 12 '13 at 21:08
    
Metaprogramming is ubiquitous. Any opensource project relying on Autotools is using metaprogramming. MOC in the Qt world, tblgen in LLVM, T4 in numerous .NET projects, pretty much any web project generating JavaScript, and so on. –  SK-logic Nov 13 '13 at 10:19
add comment

4 Answers

If you consider reflection a kind of meta-programming, it is relatively common. For some people even generic programming (templates and generics) is a form of meta-programming, so that's even more common.

In my opinion, however, meta-programming is something more complex, that involves actual code generation, and therefore quite uncommon, even in scripting languages.

share|improve this answer
    
Why template metaprogramming is not an "actual code generation"?!? C++ templates are Turing-complete, and you can generate arbitrary complex code. –  SK-logic Nov 13 '13 at 10:20
    
@SK-logic: I mean new code being generated on demand at runtime. Templates are processed by the compiler, you can see them as a powerful kind of precompiler macro. –  Lorenzo Nov 15 '13 at 12:53
    
why would you want to generate code in runtime? The most powerful forms of metaprogramming (like in Common Lisp, Racket, Template Haskell, etc.) are in compile-time. –  SK-logic Nov 15 '13 at 13:00
add comment

I think it depends strongly on what language you use. Common Lisp people probably make a fair use of it - the Meta-Object Protocol allows for neat things transparent/orthogonal persistence, for instance - and Smalltalkers use it frequently.

share|improve this answer
add comment

It tends to show up commonly and consistently in Common Lisp projects. Check out OnLisp and Let Over Lambda for some explanation/insight.

If you're just after examples (and know CL), check out the Antiweb repository; there's a good deal of meta-programming going on under the covers.

share|improve this answer
add comment

For the past two years the first bit of programming on a new task has almost always been writing code to write code. There's a ton of boilerplate associated with interactions with a database that can be simplified down to automatic script generation. Think of things where you need to query 3 tables to form an object or some such. Why write it yourself if there's 10 of these things that might change in the future?

share|improve this answer
add comment

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