Code Generation in Action

Jack Herrington

Mentioned 4

Describes how code generators are used in user interfaces, remote procedure access, and database access, and shows how to use programs to write other programs.

More on

Mentioned in questions and answers.

I need ERB (Ruby's templating system) for templating of non-HTML files.
(Instead, I want to use it for source files such as .java, .cs, ...)

How do I "execute" Ruby templates from command line?

You should have everything you need in your ruby/bin directory. On my (WinXP, Ruby 1.8.6) system, I have ruby/bin/erb.bat

erb.bat [switches] [inputfile]
  -x               print ruby script
  -n               print ruby script with line number
  -v               enable verbose mode
  -d               set $DEBUG to true
  -r [library]     load a library
  -K [kcode]       specify KANJI code-set
  -S [safe_level]  set $SAFE (0..4)
  -T [trim_mode]   specify trim_mode (0..2, -)
  -P               ignore lines which start with "%"

so erb your_erb_file.erb should write the result to STDOUT.

(EDIT: windows has erb.bat and just plain "erb". The .bat file is just a wrapper for erb, which I guess should make the same command work pretty much the same on any OS)

See the prag prog book discussion (starts about half-way down the page).

Note also that Jack Herrington wrote a whole book about code generation that uses Ruby/ERB.

I use MyGeneration along with nHibernate to create the basic POCO objects and XML mapping files. I have heard some people say they think code generators are not a good idea. What is the current best thinking? Is it just that code generation is bad when it generates thousands of lines of not understandable code?

Code generated by a code-generator should not (as a generalisation) be used in a situation where it is subsequently edited by human intervention. Some systems such the wizards on various incarnations of Visual C++ generated code that the programmer was then expected to edit by hand. This was not popular as it required developers to pick apart the generated code, understand it and make modifications. It also meant that the generation process was one shot.

Generated code should live in separate files from other code in the system and only be generated from the generator. The generated code code should be clearly marked as such to indicate that people shouldn't modify it. I have had occasion to do quite a few code-generation systems of one sort or another and All of the code so generated has something like this in the preamble:

-- =============================================================
-- === Foobar Module ===========================================
-- =============================================================
--         === THIS IS GENERATED CODE.  DO NOT EDIT. ===
-- =============================================================

Code Generation in Action is quite a good book on the subject.

I have being toying with the idea of creating software “Robots” to help on different areas of the development process, repetitive task, automatable task, etc. I have quite a few ideas where to begin. My problem is that I work mostly alone, as a freelancer, and work tends to pill up, and I don’t like to extend or “blow” deadline dates. I have investigated and use quite a few productivity tools. I have investigated CodeGeneration and I am projecting a tool to generate portions of code. I use codeReuse techniques. Etc. Any one as toughs about this ? as there any good articles.

Code generation is certainly a viable tool for some tasks. If done poorly it can create maintenance problems, but it doesn't have to be done poorly. See Code Generation Network for a fairly active community, with conference, papers, etc.

Code Generation in Action is one book that comes to mind.

While programming in C# using Visual Studio 2008, I often wish for "automatic" code generation. If possible, I'd like to achieve it by making my MSBuild solution file call out to Rake, which would call Ruby code for the code generation, having the resulting generated files automatically appear in my solution.

Here's one business example (of many possible examples I could name) where this kind of automatic code generation would be helpful. In a recent project I had an interface with some properties that contained dollar amounts. I wanted a second interface and a third interface that had the same properties as the first interface, except they were "qualified" with a business unit name. Something like this:

public interface IQuarterlyResults
     double TotalRevenue { get; set; }
     double NetProfit { get; set; }

public interface IConsumerQuarterlyResults
     double ConsumerTotalRevenue { get; set; }
     double ConsumerNetProfit { get; set; }

public interface ICorporateQuarterResults
     double CorporateTotalRevenue { get; set; }
     double CorporateNetProfit { get; set; }

In this example, there is a "Consumer Business Unit" and a "Corporate Business Unit". Every property on IQuarterlyResults becomes a property called "Corporate" + [property name] on ICorporateQuarterlyResults, and likewise for IConsumerQuarterlyResults.

Why make interfaces for these, rather than merely having an instance of IQuarterlyResults for Consumer and another instance for Corporate? Because, when working with the calculator object I was building, the user had to deal with 100's of properties, and it is much less confusing if he can deal with "fully qualified" property names such as "ConsumerNetProfit".

But let's not get bogged down in this example. It is only an example and not the main question.

The main question is this: I love using Ruby and ERB for code generation, and I love using Rake to manage dependencies between tasks. To solve the problem above, what I'd like to do is have MSBuild call out to Rake, and have Rake / Ruby read the list of properties on the "core" interface and then generate the code to make all the dependent interfaces and their properties. This would get triggered every time I do a build, because I'd put it into the MSBuild file for the VS.NET solution.

Has anyone tried anything like this? How did it work out for you? What insights can you share about pros, cons, tips for success, etc.?


We did it with Nant/ruby+ERB - it generates a tone of CRUD in our DAL and data entry form input for our web app. for us it works really well, but there was a good amount of setup time in the beginning that made my boss/customer nervous becuase they figured we should be showing more production than prototype.

Also be sure what the generation is doing - there are differences between generate & modify and generate + integrate. generate & modify is more like using a wizard and is one off. when you generate + integrate you need to understand where/how you will exploit the generated code either through inheritance or through library calls from hand written code into the generated code.

Ruby/erb is not fast executing. A method to determine when you do/dont have to generate are helpful becuase compile cycles start to become long and unproductive. We also found it helpful to always build a small test case to generate even a single set of artifacts before integrating it into the work flow - you have a test of the product and it doesnt slow down anyone while you work on the cg element(s).

I would read Jack Herringtons Code Generation in Action as its geared toward Ruby. Its got me into CG.

I would also rea Kathleen Dollards Code Generation in Microsoft .NET. It built for using XSLT but the principles are the same for developing the metdadata, transformation and integration stages. Her articles are also helpful that you find in magazines and around the web.