Wednesday, February 18, 2009

Code Generators: Robots of Software Factory

There are many analogies between software development and manufacturing. “Software Factory” methodology utilizes these similarities. Another analogy is possible for construction industry. As an interdisciplinary study, these similarities are beneficial to understand our own software problem. In manufacturing industry, robots replaced the workers in some product lines and workers did controlling and managing of these robots. Robots are great examples of automation. They are configured for the current assembly-line product specifications. They don’t have to be “Artificially Intelligent” to make the job. Analogy is that code generators have similar features with industrial machinery robots. You program the generators according to your application specification and your software developers use these tools to produce the software (also named as Generative Programming). In this way, we can automate software development.

There are many types of code generators and techniques. Some of them low-level techniques that helps achieving some specific tasks. One example is language abstraction. Your high-level language is automatically translated-generated into low-level machine code. One example in Java world is JSP transformation into servlet. These generators generally invisible to the developers and can’t be considered as an automation in development.

The generators that provide automation are those generate final software that the software team develops. In business applications, final software is composed of elements like GUIs, business logic codes and database codes. Every elements of software may be taken into generators if you can capture template or pattern. Templates are some specification patterns that depend on your API set. Let’s say templates are meta-descriptions of your coding standards.

“To build a generator or use existing one” is one of the most discussed issue in your software team. Could we utilize the advantages of generators or could it be a wrong investment that ruin clean code base? Yes critical question lies behind the meaning of generated code for your team. If generated code value is high that it shortens development time by removing tedious repetitive tasks than it would be worth for investment.

Code generation can be applied in following way:

1- Determine what to generate: To find repetitive task is not hard in your development environment. If same things are done more than three times it is a candidate to generate. For example, generator experts recommend code generation for CRUD codes.

2- Capture meta-level coding structure: Capturing your meta-coding structure is important. Your coding structure should have a pattern that is shared among applications. What are the variable parts that would be parameterized by generator? How your GUI is constructed? In which way do you access database? How does your controller code manage object states? Capturing is the hardest part of the whole job. It requires understanding of the current development API set very well.

3- Build or configure your generator: There are many generators available there, but if not satisfied with them even you can write it yourself. After building it should be tested against current codes if anything is wrong. Generated code should be complete, concise and error-prone. Otherwise leaky generators make it impossible to benefit generation technology. Your coders would prefer manual way instead of wrestling your generators.

My generator recommendations:
  • Generators should not change generated code structure a lot. Otherwise, “Coding standards are kept” benefit would be lost.
  • If we find an abstraction area, we should ask if this abstraction is done via code generator or a new API.
  • Generated code should as short as possible. It should be supported with a high-level API set to reduce produced code.
  • Every part of software can be generated. There is no limit. Limit is your capturing ability in your environment.
  • I don’t like “Your code is here” generators. IMHO, generated code can be changed in every way. Changing it should be safe as well. Dynamic generators that can change code base after customizations may restrict this. Static generators may provide much more freedom with re-generation overwrite problem. If we change it, it should be readable and understandable.
  • More intelligent generated codes mean more intelligent generators. Generators complexity should be manageable. Here MDA rescues. Models are a way to achieve sophisticated generation in a meaningful way. Generators should use models (i.e. database models).
  • Code generation should not be made a rocket science. Begin with small steps than make improvements. It should serve you, not tie you.
  • Code generation speeds prototyping thus serves for “Agile Development”. Any change in software specification can be easily reflected into prototype without heavy re-write.


    Code Generation in Action

    Generative Programming: Methods, Tools, and Applications

  • No comments: