Thursday, May 19, 2005

Is code generation an agile technique?

Code generation friend or foe?

Usually in the agile world code generation is considered wrong. In order to be able to contradict this belief we must go to its roots.

In my opinion, code generation has two main reasons why is usually considered wrong:

1: Tools that generate uncontrollable code

Throughout the years, many IDEs and tools generated code. For the beginners and for marketing people this was the panacea but experienced developers quickly came to the conclusion, that these tools were doing more damage then good. This was usually because the generated code either underperformed, or was poorly written, generation could not be properly controlled or as soon as the application was getting more complex, the generated code was far from a solution. Debugging and changing generated code was difficult. This led to the raise and fall of CASE tools.

The only code generation tools that seem to have lasted over the years are the UI designers, although there are still disputes whether you should do the HTML 'by hand' or using a tool like Dreamweaver or FrontPage. My opinion is that probably as in most cases, the truth is somewhere between. Use the tool, but also control everything by hand.

2. People generate code before they can afford it

Sometimes, beginners start generating the code before really understanding what it must do. For instance, recently I saw a few fellow developers adopting a new ORM and found out that there is a tool that can generate the mapping xml files. They started with the tool, but as they didn't really have the knowledge to understand what should be generated, how this mapping files really work, ended up in redoing the mapping files by hand and taking everything step by step just to understand how it works.

The good

In my opinion code generation should be applied in a totally different
way, and this way is in my opinion the agile way.

Steps to use code generation as an agile technique:

STEP 1. Develop by hand and get a very good understanding of the domain and
of the code

STEP 2. If the code can be generated, then why not.

Code generation, can drastically improve the speed of a project. This is a very powerfull weapon that developers can use, but is preferable to be carefully studied before starting to shoot it. Just like any powerful weapon, if used wrong it can have devastating effects.

It is very useful in changing environments, when parts
or the code when a change is done is generated. This is especially
useful in testing user interfaces. Much code can just be regenerated,
when the interface changes, which makes maintainability much easier, and thus automating user interface tests much more friendly.


Because code generation, can help in changing environments, can help the speed of development I think that code generation is the big missing agile technique from any methodology. But like most agile techniques that are also very powerful should be approached with care , remembering that it does not replace coding manually , just help it and it can by no means be used in environments where the base knowledge has not been acquired yet.

List of available code generators for .NET:

1 comment:

Mircea said...

I totally agree with you Dan. Especially when you say: you have to generate and then verify everything by hand.
I remember the times when I wrote websites in HTML. I tried to use FrontPage, but I didn’t like it because I couldn’t have the control I wanted over the final HTML file. So I used FrontPage only to generate let’s say a template of the page, then I took it in an editor and continued to compose the page.
There is slight problem. If you generate a lot of code, it will be very hard to check it. And probably you will loose a lot of time. But I agree that we have to find a way between generating and writing code.