Peter D. Hedderley
Technology Specialist & IT Consultant



"Software is like a lucid dream, limited only by our imagination and ingenuity
- we are the architects, sculptors, rule-makers and rule-breakers."
Peter D. Hedderley.

User Tools

Site Tools


articles:keeping_code-generation_in_mind

(Back to Articles)

Let Me Repeat Myself! - Keeping Code-Generation in Mind

Overview

Build-Time Code Generation and Run-Time Data Loading techniques help to decouple real implementation issues from data-related issues. The tools and concepts presented in this article go a long way to simplifying the creation of data-driven code and to reducing the effort required to maintain code in the long term.

Take a look at this Java snippet:

Example.java
package com.hedderley.example;
 
public class Example
{
   private static List<String> keywords = new ArrayList<String>();
 
   static 
   {
      keywords.add("keyboard");
      keywords.add("pedal");   
      keywords.add("couple");   
      keywords.add("register");   
   }
}   

For a short 4-entry definition, we might decide to go along with the current approach, however as soon as our needs change, we will find ourselves modifying code unnecessarily and unproductively.

Coding styles and other such issues asside, we can immediately identify a repeating pattern in our code - a perfect case for code generation or externalized data.

Using build-time code generation (let's say, through some Velocity-based system), our code might look like this:

Example.java.codegen
package com.hedderley.example;
 
public class Example
{
   private static List<String> keywords = new ArrayList<String>();
 
   static {
##for($keyword in ${data.keywords})
      keywords.add("${keyword}");
##end
   }
}   

And using run-time data loading (let's say, through some Data-Driven Framework), our code might look like this:

Example.java
package com.hedderley.example;
 
import com.hedderley.datadriven.CSVArrayList;
 
public class Example
{
   private static List<String> keywords = new CSVArrayList<String>("/com/hedderley/example/resources/keywords.csv", String.class);
}   

Two Approaches

Build-Time Code Generation

Assumptions

  • All data is known and available at compile/build time and will not change at run time.
  • Availability of a code generation tool.

Advantages

  • Will benefit from compiler optimizations.
  • Compile-time syntax validation.
  • Results are immediately available at compile-time, for other code which may depend upon them.
  • Results are real code and are thus not limited to being data structures.
  • Can address very complex scenarios, potentially generating any form and any amount of code.

Disadvantages

  • Slower build process.
  • For simple situations, a code-generation template may cause some minor level of unreadability.
  • Will bloat the runtime footprint.

Run-Time Data Loading

Assumptions

  • Availability of a data loading mechanism/framework.

Advantages

  • Data is loaded at run-time and therefore could be made user-definable/configurable.
  • No requirement for data to be available at compile time.
  • Use of in-place data loading code may simplify the code and increase readability.
  • Can be loaded on-demand, without unnecessarily bloating the runtime footprint.

Disadvantages

  • Will not be able to benefit from compiler optimizations.
  • May slow down run-time execution during loading-phase.
  • Only suitable for relatively simple situations and data structures.
  • Results of data-loading are data structures - generally no option for producing code-related things (e.g. classes, interfaces, methods).

Tools for the Job

In looking for appropriate tools, I specified the following search criteria:

  • General Requirements
    • MUST be Open Source - with usage allowed for commercial projects.
    • MUST be easy to use and understand, simple configuration.
    • SHOULD be as self-contained as possible, with as few dependencies as possible (ideally, none).
  • Code Generation Framework Requirements
    • MUST work for any target language or text-based file format.
    • MUST be able to generate single files and multiple files.
  • Data-Loading Framework Requirements
    • MUST be suitable for Java.
    • MUST EITHER be suitable for C# or be easily portable to C#.
articles/keeping_code-generation_in_mind.txt ยท Last modified: 2012/09/10 13:31 by Peter Hedderley