Ask HN: Code Generators

2 points by jjonte ↗ HN
I am interested in knowing which (if any?) code generation tools Hacker News readers use?

As a professional C# developer (don't laugh :), I'm familiar with CodeSmith, MyGeneration, and most recently Microsoft T4. But I'm getting more and more into Python, PyDev, and Eclipse and would like to generate Python source based on entity definition files. I have rolled my own ORM toolkit in C# would now like to "port" it to Python.

I came across Cheetah and it looks pretty solid, but an IDE-integrated solution would be ideal. Is there a tool which is the code generator to use? Or maybe because there is no obvious solution, code generation with dynamically-typed languages is dead and I'm trying to apply my statically-typed background to a dynamically-typed foreground?

7 comments

[ 5.0 ms ] story [ 31.2 ms ] thread
I think compilers exist largely to relieve humans of the tedium and difficulty of dealing with the target language. Accordingly, I'm more likely to look for a better compiler or a more flexible metaprogramming language or approach than to seek/invent a source code generation program.
About 4 years ago I was assigned to an internal project that was to provide a GUI CRUD interface in VB.Net to replace one written in curses on Linux. There were 50-75 attributes for the main entity plus multiple hierachical subsets of sub-entities. It was a mess. The business user specified a complex GUI that would let you have different levels of users and allow fields to be protected and show warnings and error by changing the background color of the control, etc, etc. Tons of boilerplate code and lots of changing conditions because the business user would come up with new requirements and conditions to test for.

To keep track of all of this I ended up specifying a lot of the rules in an XML file and used 'Cog' written by Ned Batchelder. http://nedbatchelder.com/code/cog/ to generate the boilerplate code. Cog is different than any other code generation tool I had used before because you write your generation code in commented lines in your normal source file. When you run Cog, it finds these special lines and replaces the code between the markers with whatever code is supposed to be generated. No other code is touched so it survived a round trip through an IDE like VS. It worked great for what I was doing and I used it heavily but the project was thankfully cancelled before I could finish.

I use Common Lisp, which is the code generation tool to end all code generation tools. I used to use the kind of thing you mention, but now it all looks like unpleasant workarounds for the lack of an extensible language and compiler. I realize you may not be on that path, but seriously, since you're interested in these questions, learn it someday. If your experience is like mine, how you think of programming will get much clearer and simpler.
"code generation with dynamically-typed languages is dead and I'm trying to apply my statically-typed background to a dynamically-typed foreground?"

THIS

Don't ever generate boilerplate Python code (especially 'fill in the blanks' crap). You're using a language where you can put the data in an appropriate structure used by high-order code.

You don't even have to make a new data structure if the information is already present. For instance, I wrote a python library that when called as an executable, looks up 'tool verb' style commands by looking at the methods on the available classes, gets the help from __doc__, and the extra optargs from attributes on the methods (added via a decorator).

Interesting, that kind of "smelled" like the right answer.

So, help me out here - "You're using a language where you can put the data in an appropriate structure used by high-order code." I don't understand that statement? I don't know what "the data" or "high-order code" is?

The entity definition files are the data. Higher-order code is code that operates on code. I need a slightly more concrete example from you to get any more concrete here, but I'm imagining that the C# code generator has a set of rules for mapping entities to 1) C# classes and 2) fields in a database, and the generated C# classes automatically do database queries upon method/attribute accesses. In Python, you might override attribute lookup on your "DatabaseObject" class to hit the database, or you could have some code that processes an entity definition file and adds appropriate functions to Python classes at runtime. The advantage is that you don't have reams of code to muddle through and compile.
While your Python advice seems sound (as far as I can tell in my ignorance), it's obviously not true that "code generation with dynamically-typed languages is dead". These two things are orthogonal. Dynamic languages can be, and are, compiled.

It's true that in less powerful languages, you need code generation to do what more powerful languages can do with higher-order functions and reflection. But it's also true that code generation (along with other techniques from compiler and language development) remain quite useful for writing shorter, clearer programs in environments that support this.