Automatic programming: write code that writes code
After reading the book The Pragmatic Programmer, one of the arguments I found most interesting was "write code that writes code".
I tried searching over the net for some more explanations or articles about it, and while I found some good articles on the subject, I still haven't found any specific code implementation or good examples.
I feel it's still not a so common argument, something that lacks documentation or it's not embraced by so many people, and I would like to know more about it.
What do you think about the subject? Is it something that will really increase your productivity? What are some good resources on the subject, among books, blogs, slideshows, etc?
Some code examples would be greatly appreciated to allow me to better understand its implementation.
Here's the wiki page on the subject with various relevant programming techniques, like Meta Programming, Generative Programming and Code Generation.
@check132: Yes, but I'm talking about writing code yourself that in turn generate code, I guess to simplify or automate some repeating tasks, while you can focus less on the code details (specific syntax for example) and more on the overall project. In the book example, they wrote one function able to write the same code in two different languages.
Is this akin to entering a trance state and engaging in 'automatic writing'? http://en.wikipedia.org/wiki/Automatic_writing
I wrote code to spit out optimized matrix math functions yesterday. Can't say I can think of many reasons to think of code generation as a practice more than as a tool.
Philip Greenspun has a nice simple example 'Metadata (and Automatic Code Generation)' at http://philip.greenspun.com/seia/metadata
AtomWeaver (http://www.atomweaver.com) is a good example of automatic programming: First, you create reusable mini-programs in Lua. Then, you model your system by reusing these assets. AtomWeaver then weaves a Lua program that contains your "mini-generators" to generate the system's final source code. You can then tweak your model and re-generate.
According to this answer to a question re what language the Mars Curiosity rover was written in, the software consists of 3.5 million lines of C, much of it auto-generated. "Only" one million lines of C were hand-coded.
Here's a practically applied pipe optimization library: https://github.com/heya/pipe . Check out why its cool starting from slide 30 here and slide 40 that shows you a heya pipe example: https://www.slideshare.net/elazutkin/practical-pairing-of-generative-programming-with-functional-programming . The idea is that you can write functional pipes in a way that it's compiled and things appropriately inlined and otherwise optimized to give you far faster code without losing code readability.
In the Lisp world, it is quite common to see the code which writes code which writes code (and so on). So, any decently sized Lisp or Scheme project will serve as a good code example. I'd recommend looking at the Racket compiler and runtime sources, as well as Bigloo, their libraries are just brilliant.
As for the productivity: I'm using metaprogramming as a dominant technique in almost all of my development work, and it clearly helps a lot, both reducing the code size and increasing its readability. The key is in using Domain Specific Languages, and metaprogramming is one of the most efficient ways of implementing them.