Correl Roush has a very nice post that beautifully illustrates the idea of writing code to write code. It’s a tremendously powerful idea because it lets you leverage some other program or programming language to do a task that requires specialized knowledge to code or is otherwise too hard to do directly. Examples that I’ve used are writing code to generate troff source as an intermediate to producing typeset output and code to generate graphviz source as an intermediate to generating a graph.
Roush gives us a nice example of generating graphviz (dot) source code to produce a nice looking graph. The particular problem she’s addressing is building a tool that you can point at a git repository and have it produce a nice graph of the commits. That seems unreasonably hard if you approach the problem ab initio but is very tractable if you leverage git and graphviz to do the hard work. Git can produce a topologically sorted list of commits and graphviz can do all the drawing of the graphs. All Roush has to do is add some glue code.
Roush decided to work in elisp and builds up her application step by step. She starts by showing the dot
input she wants to produce and then writes the code to produce it. By iterating through more complicated graphs until she gets the final result, she shows to build the application iteratively, a programming method that I especially like.
I also like the way Roush uses data abstraction. Her list-based implementation may or may not scale to huge git repositories but her use of data abstraction makes it almost trivial to move to a more efficient implementation if required.