Code Generation in Action
Jack Herrington
  • July 2003
  • ISBN 9781930110977
  • 350 pages

will open your eyes to the potential of code generation ... insightful and practical

John Lam, co-author of Essential XML

This book is no longer available as a pBook. However, you can still purchase the eBook!


Code Generation in Action covers technique and implementation for building high-quality machine-generated code for today's complex applications frameworks. The book includes step-by-step instruction for building dozens of code generators of varying types. These generators build high-quality output that is consistent and maintainable. Code generation abstracts the design of the code so that multiple outputs can be created from a single model of the application functionality, which means development teams can focus on higher-level design work and strategic problems, while still meeting goals for maintaining production applications. The book covers techniques that range from simple code processors that handle common coding problems to more elaborate and complex generators that maintain entire application tiers.

Table of Contents detailed table of contents

preface

acknowledgments

about this book

about the title

about the cover illustration

Part I Code generation fundamentals

1. Overview

1.1. A generation case study

1.1.1. Step 1: generating the database access layer

1.1.2. Step 2: generating the user interface

1.1.3. Step 3: building the client interface

1.1.4. Step 4: building unit tests

1.1.5. Step 5: integrating a technical change

1.1.6. Step 6: integrating a design change

1.1.7. Step 7: building an RPC layer

1.1.8. Step 8: building documentation

1.1.9. Case study conclusions

1.2. Benefits of code generation for engineers

1.3. Benefits of code generation for managers

1.4. The code generation process

1.4.1. Assessing the need

1.4.2. Laying down the infrastructure

1.4.3. Documenting it

1.4.4. Deploying it

1.4.5. Maintaining it

1.5. The buy/build decision

1.6. Code generation at its best

1.7. Top ten code-generation rules

1.8. Generators you are using today

1.9. Summary

2. Code generation basics

2.1. The various forms of active code generation

2.1.1. Code munging

2.1.2. The inline-code expander

2.1.3. Mixed-code generation

2.1.4. Partial-class generation

2.1.5. Tier or layer generation

2.1.6. Full-domain language

2.2. Code generation workflow

2.2.1. Editing generated code

2.3. Code generation concerns

2.3.1. Nobody on my team will accept a generator

2.3.2. Engineers will ignore the "do not edit" comments

2.3.3. If the generator can’t build it, the feature will take weeks to build

2.4. Code generation skills

2.4.1. Using text templates

2.4.2. Writing regular expressions

2.4.3. Parsing XML

2.4.4. File and directory handling

2.4.5. Command-line handling

2.5. Choosing a language for your code generator

2.5.1. Comparing code generator languages

2.5.2. Using Ruby to write code generators

2.6. Summary

3. Code generation tools

3.1. Building generators with Ruby

3.2. Parsing XML with Rexml

3.2.1. Using Rexml

3.3. ERb: a great tool for templates

3.3.1. Installing and using ERb

3.3.2. ERb templates

3.3.3. Building ASP, JSP, etc. with ERb

3.4. Building the language parser toolkit

3.4.1. Inside the Tokenizers

3.4.2. Inside the LanguageScanners

3.4.3. Language parsing tools

3.5. Summary

4. Building simple generators

4.1. The code munger generator type

4.1.1. Uses and example code

4.1.2. Developing the generator

4.2. The inline-code expansion generator model

4.2.1. Uses and examples

4.2.2. Developing the generator

4.3. The mixed-code generator model

4.3.1. Uses and examples

4.3.2. Developing the generator

4.4. The partial-class generator mode

4.4.1. Uses and examples

4.4.2. Developing the generator

4.5. The tier generator model

4.5.1. Uses and examples

4.5.2. Developing the generator

4.6. Generating for various languages

4.6.1. C

4.6.2. C++

4.6.3. C#

4.6.4. Java

4.6.5. Perl

4.6.6. SQL

4.7. Summary

Part II Code generation solutions

5. Generating user interfaces

5.1. The big picture

5.1.1. Why generate the user interface?

5.1.2. Integration with existing tools and other generators

5.2. Designing a good interface

5.3. Workflow comparison

5.4. A case study: generating JSP

5.4.1. Why generate JSPs?

5.4.2. The role of the generator

5.4.3. The high-level architecture

5.4.4. A table definition file

5.4.5. A form definition file

5.4.6. Processing flow

5.4.7. Designing the generator

5.5. Technique: generating Swing dialog boxes

5.5.1. Generator requirements

5.5.2. Architectural recommendation

5.5.3. Processing flow

5.6. Technique: generating MFC dialog boxes

5.6.1. Generator requirements

5.6.3. Processing flow

5.6.5. Processing flow

5.7. Design tips

5.8. Tools for generating UIs

5.9. Summary

6. Generating documentation

6.1. The importance of structured comments

6.2. The big picture

6.3. A case study: augmented SQL code

6.3.1. Roles of the generator

6.3.2. Laying out the generator architecture

6.3.3. Processing flow

6.3.4. Building the code for the SQL generator

6.3.5. Performing system tests

6.4. Understanding the JavaDoc and the Doclet API

6.5. Finding a tool to do it for you

6.6. Summary

7. Generating unit tests

7.1. The big picture

7.2. Preparing for common concerns

7.2.1. Unit tests won’t be maintained

7.2.2. It’s too hard to make unit tests

7.2.3. Unit tests will limit our flexibility

7.2.4. The interface isn’t ready yet

7.2.5. Unit tests are not worth the time

7.2.6. Adding unit tests to a legacy system is painful

7.2.7. Unit tests can only prove the existence of errors

7.3. A case study: augmented C code

7.3.1. Roles of the generator

7.3.2. Laying out the generator architecture

7.3.3. Processing flow

7.3.4. Building the code for the unit test generator

7.3.5. Finding tools for test cases

7.4. Technique: the ordered test generator

7.4.1. Roles of the generator

7.4.2. Laying out the generator architecture

7.4.3. Processing flow

7.5. Technique: the test data generator

7.5.1. Roles of the generator

7.5.2. Laying out the generator architecture

7.5.3. Processing flow

7.5.4. Using the test data generator and the sequential test generator together

7.6. Technique: the test robot generator

7.6.1. Roles of the generator

7.6.2. Laying out the generator architecture

7.6.3. Processing flow

7.6.4. Generators for robots

7.7. Finding a tool to do it for you

7.8. Design tips

7.9. Summary

8. Embedding SQL with generators

8.1. PerlSQL

8.2. Preparing for common concerns

8.2.1. We are creating a new language

8.2.2. Debugging will be a pain

8.2.3. With Perl you could do this within the language

8.3. Workflow comparison

8.4. A case study: PerlSQL

8.4.1. Roles of the generator

8.4.2. Laying out the generator architecture

8.4.3. Processing flow

8.4.4. Building the code for the PerlSQL generator

8.4.5. Performing system tests

8.5. Finding SQL tools

8.6. Summary

9. Handling data

9.1. Code generation versus a library

9.2. A case study: a simple CSV-reading example

9.2.1. Roles of the generator

9.2.2. Laying out the generator architecture

9.2.3. Processing flow

9.2.4. Building the code for the CSV reader generator

9.2.5. Performing system tests

9.3. Technique: data adapters

9.3.1. Roles of the generator

9.3.2. Laying out the generator architecture

9.3.3. Processing flow

9.4. Technique: binary file reader/writer

9.4.1. Roles of the generator

9.4.2. Laying out the generator architecture

9.4.3. Implementation recommendations

9.4.4. Processing flow

9.5. Finding tools to do it for you

9.6. Summary

10. Creating database access generators

10.1. Benefits of database access generators

10.2. The big picture

10.2.1. Terminology

10.2.2. Integration with existing tools

10.2.3. Working with other generators

10.3. Preparing for common concerns

10.3.1. The code is going to be out of control

10.3.2. I’m going to be the only one who knows what’s going on

10.3.3. Our application semantics aren’t well defined yet

10.3.4. This is going to take all the joy out of coding

10.3.5. The database design is too complex to be generated

10.3.6. The generated SQL SELECT statements will be rubbish

10.3.7. The up-front development cost is too high

10.3.8. I don’t have all of the prerequisite skills

10.3.9. The information here is centered around web applications; what about client/server?

10.3.10. My application doesn’t use a database

10.4. Workflow comparison

10.5. A case study: EJB generation

10.5.1. The architecture of the EJB database access layer

10.5.2. EJB options

10.5.3. The schema

10.5.4. Roles of the generator

10.5.5. Laying out the generator architecture

10.5.6. Processing flow

10.5.7. The schema definition

10.5.8. The extensions definition file

10.5.9. Sample data definition

10.5.10. Implementation

10.5.11. Performing system tests

10.5.12. J2EE developer resources

10.5.13. Generators for J2EE

10.6. Technique: generating JDBC

10.6.1. Roles of the generator

10.6.2. Laying out the generator architecture

10.6.3. Processing flow

10.6.4. JDBC developer resources

10.7. Technique: generating database access layers for ASP

10.7.1. Roles of the generator

10.7.2. Laying out the generator architecture

10.7.3. Processing flow

10.7.4. ASP developer resources

10.8. Technique: generating database access layers for ASP.NET

10.8.1. Roles of the generator

10.8.3. Laying out the generator architecture

10.8.4. Processing flow

10.8.5. ASP.NET developer resources

10.8.6. Generators for .NET

10.9. Technique: Generating database access classes for Perl DBI

10.9.1. Roles of the generator

10.9.2. Laying out the generator architecture

10.9.3. Processing flow

10.9.4. Perl/DBI developer resources

10.10. Technique: generating database access classes for PHP

10.10.1. Roles of the generator

10.10.2. Laying out the generator architecture

10.10.3. Processing flow

10.10.4. Generators for PHP

10.10.5. PHP developer resources

10.11. Off-the-shelf: AndroMDA

10.11.1. Roles of the generator

10.11.2. Laying out the generator architecture

10.11.3. Developer resources

10.12. Off-the-shelf: XDoclet

10.12.1. Roles of the generator

10.12.2. Laying out the generator architecture

10.13. Design tips

10.14. Finding a tool to do it for you

10.15. Summary

11. Generating web services layers

11.1. The big picture

11.1.1. Providing security and authentication

11.1.2. Why not use a library?

11.2. A case study: generating XML-RPC for Java

11.2.1. The XML-RPC message flow

11.2.2. Roles of the generator

11.2.3. Laying out the generator architecture

11.2.4. Processing flow

11.2.5. Building the code for the XML-RPC generator

11.2.6. Performing system tests

11.2.7. XML-RPC resources

11.3. Technique: generating SOAP

11.3.1. Roles of the generator

11.3.2. Laying out the generator architecture

11.3.3. Processing flow

11.3.4. SOAP resources

11.4. Design tips

11.5. Finding a tool to do it for you

11.6. Summary

12. Generating business logic

12.1. A case study: generating Java business equations

12.1.1. Roles of the generator

12.1.2. Laying out the generator architecture

12.1.3. Processing flow

12.1.4. Building the code for the equation generator

12.1.5. Performing system tests

12.2. A case study: the report logic and interface generator

12.2.1. Roles of the generator

12.2.2. Laying out the generator architecture

12.2.3. Processing flow

12.2.4. A report logic and interface generator alternative

12.2.5. Report logic generation tools

12.3. Summary

13. More generator ideas

13.1. Technique: maintaining header files

13.1.1. Roles of the generator

13.1.2. Laying out the generator architecture

13.1.3. Processing flow

13.2. Technique: creating DLL wrappers

13.2.1. Roles of the generator

13.2.2. Laying out the generator architecture

13.2.3. Processing flow

13.3. Technique: creating wrappers for external languages

13.3.1. Roles of the generator

13.3.2. Laying out the generator architecture

13.3.3. Processing flow

13.4. Technique: creating firewall configurations

13.4.1. Roles of the generator

13.4.2. Laying out the generator architecture

13.4.3. Processing flow

13.5. Technique: creating lookup functions

13.5.1. Roles of the generator

13.5.2. Laying out the generator architecture

13.5.3. Processing flow

13.6. Technique: creating macro lookup tables

13.6.1. Feeding code to the generator

13.6.2. Roles of the generator

13.6.3. Laying out the generator architecture

13.6.4. Processing flow

13.7. Summary

Appendix A: A brief introduction to Ruby

Appendix B: The simple system test framework

Appendix C: EJBGen code and templates

Appendix D: Integrating code generation into your IDE

Appendix E: Simple templating

Appendix F: Patterns for regular expressions

index

About the book

Code Generation in Action covers building database access, user interface, remote procedure, test cases, and business logic code as well as code for other key system functions.

Although code generation is an engineering technique it also has a large impact on the engineering team and management. The book discusses the non-technical justifications for code generation in depth, and offers practical advice for making code generation succeed in any organization.

What's Inside:

  • Code generation models
  • Practical examples of database access generation
  • Architectures for generators for all of today's popular technologies
  • Insight into deployment issues

Direct examples are provided on a variety of platforms. These include Java/J2EE, Microsoft's ASP and ASP.NET, as well as Open Source solutions such as Perl, Python and PHP.

What's inside

  • Code generation basics
  • CG techniques and best practices
  • Patterns of CG design
  • How to deploy generators
  • Many example generators

Over his twenty years of development experience, Jack Herrington has shipped many software applications helped by code generation techniques.

Includes generators for

  • Database access
  • RPC
  • Unit tests
  • Documentation
  • Business logic
  • Data translation

About the reader

Who can benefit from this book:

  • Senior engineers looking for ways to improve their productivity and the quality of their work.
  • Systems architects who want to maintain their design decisions in abstract form and then generate code to match the design.
  • Product Managers and Project Managers who wish to understand the design principles and cultural benefits of code generation techniques.

About the author

Jack Herrington is a software engineer with 20 years of experience developing applications using a diverse set of languages and tools. He has shipped applications from scientific real-time applications to web applications for business. He is an expert in the use of code generation techniques from very simple code maintenance to code generators, which build entire tiers of functionality. The author lives in Union City, California.


Buy
eBook $35.99 PDF only

FREE domestic shipping on three or more pBooks

... an endless stream of ideas on how to automate the repetitive aspects of programming

Michael Lenaghan, Frogware Inc.

Like unit testing, once you try generative programming, you?ll never look back. This book will become a classic.

Richard Rodger, Jostraca