About this Book – Code Generation in Action

About this Book

This book is designed for technical professionals involved in building and shipping computer software. Our intended audience includes anyone involved in the technical development process, from front-line engineers, to architects, to technical team leaders. The code generator examples are written using the Ruby programming language. Although you may be unfamiliar with Ruby, you should be able to read and understand the code if you know any modern imperative programming languages (e.g., C, C++, C#, Java, Perl, or Python). In addition, the chapters contain target code in a variety of languages, including C, Java, and SQL. Some familiarity with these languages is required.

The term code generation is often used to refer to the back end of a compiler, but this is not how it is used in this book. We use the term to refer to the building of high-level code (e.g., C, C++, Java, Perl, Ruby, HTML) using generators that read definitions stored in code or files.

Code generation has a wide variety of applications, but compilation is not one of them. If you are looking for ways to use code generation in a compiler, I suggest you read Compilers: Principles, Techniques, and Tools, by Alfred Aho, Ravi Sethi, and Jeffrey D. Ullman (Addison-Wesley, 1986).

Chapter roadmap

This book is organized into two parts. The first part introduces the basics of code generation and shows some simple examples of the various models we present. The second part applies the code generation models to common coding cases, such as documentation, test cases, database access, and so forth.

Chapter 1: Introducing code generation

The introductory chapter provides an overview of the problem-solving capabilities of code generators, and also examines cultural concerns.

Chapter 2: Code generation basics

In this chapter we provide a foundation of code generation models that we will use throughout the book.

Chapter 3: Code generation tools

This chapter covers the tools we use in this book and the skills you need to be able to develop your own generators.

Chapter 4: Building simple generators

The last chapter in part 1 shows a number of simple generators that solve small but realistic engineering problems. You can use these simple generators as starting points to build more elaborate generators.

Chapter 5: Generating user interfaces

Building high-quality and consistent user interfaces quickly is a problem to which we apply the code generation technique in this chapter. The case study provides a complete component-based generator for building JSP interfaces from an abstract definition of the interface requirements.

Chapter 6: Generating documentation

JavaDoc is an example of using code generation to build documentation from source code. In this chapter, we discuss how JavaDoc works and then apply it to SQL documentation.

Chapter 7: Generating unit tests

This chapter applies code generation techniques to building and maintaining unit test code. This makes it easy to build quality test cases and keep them updated as the code changes.

Chapter 8: Embedding SQL with generators

Simplifying SQL work has been a goal of a number of commercial products, such as Pro*C and SQLJ, which embed SQL in C and Java, respectively, and then use a generator to build production C and Java code. This chapter describes how these generators are built and used.

Chapter 9: Handling data

Reading, writing, importing, exporting, and converting data are common engineering problems. This chapter provides code generation solutions for all of these problems.

Chapter 10: Creating database access generators

Database access code is the most commonly generated code because of the primary importance of database code and the ease with which it can be generated. This chapter covers both custom and off-the-shelf generation solutions for several implementation languages.

Chapter 11: Generating web services layers

Providing web services layers for applications is a growing field. In this chapter, we apply code generation to building high-quality web services layers.

Chapter 12: Generating business logic

Business logic isn’t usually a generation target, but with some creativity you can go a long way with generating business logic code. This chapter applies that creativity to provide pragmatic generation solutions to building stronger business logic layers.

Chapter 13: More generator ideas

This final chapter in the solutions portion of the book provides several examples of small generators applied to specific problems, such as generating firewall configurations, DLL wrappers, lookup tables, lookup functions, and more.

Appendix A: A brief introduction to Ruby

The generators in this book are written in Ruby. This appendix offers a brief introduction to the Ruby syntax as well as some more advanced concepts.

Appendix B: The simple system test framework

Testing your generator is the key to confidently maintaining and extending the generator. This appendix describes the system test framework that we use on all of the generators in the book.

Appendix C: EJBGen code and templates

This appendix provides the code for the case study generator described in chapter 10.

Appendix D: Integrating code generation into your IDE

Ease of use is critical to ensuring that your generators are used. This appendix shows how you can integrate your generator into some of the popular IDEs to make it easy for other engineers to use.

Appendix E: Simple templating

Sometimes using a text template tool like XSLT, JSP, ASP, or HTML::Mason is too much for a small project. This appendix provides powerful text-templating tools that you can implement in just a few lines of code.

Appendix F: Patterns for regular expressions

Code generators use a lot of text processing, so regular expressions are key. This appendix offers information on using regular expressions in all of the popular languages.

Source code and errata

Source code for our examples, as well as some source code not shown directly in the book, is available at the book’s web site, www.manning.com/herrington or at www.codegeneration.net/cgia. The latter is a site managed by the author and there you will find additional articles, interviews, and downloads of interest.

We purposely reduced the number of comments in the source code; the markup around the code acts as documentation. This is not to say that your generator code has to look like the code in the book; you should follow your commenting standards. Fully commented source is available on both web sites.

We have been thorough in our editing, but some errors may have made it to the printer’s press. You can check for errata for this book at www.manning.com/herrington or www.codegeneration.net/cgia/errata. You can also submit any errors you might find in this book.


In the text, Courier typeface is used to denote code. In a concession to the unique nature of code generators, text that is destined to go to generated output is shown in italic Courier. This makes reading code embedded within code a little simpler. In the example

print "for( int x=0;x<20;x++) {\n";

the for loop is going to the output file, so it is shown in italics. Because the host print operation is part of the generator, it is presented in regular Courier font. Despite the typographic differentiation, discriminating between output code and the code that generates it is not usually an issue because the generator is often written in a language different from that of the output code.

When an important new term is introduced, we use italics.

Author Online

Code Generation in Action is supported by an Internet forum, where you may interact with the author and other readers of this book. To access the forum and subscribe to it, point your web browser to www.manning.com/herrington or www.codegenerationinaction.com. There you will find a link to the forum and registration instructions.

Community support is vital for building and encouraging the adoption of code generation techniques. If you find the ideas presented in the book valuable, we would love to hear about it. If you are having trouble with the concepts, we want to hear that as well.

About the author

Jack Herrington is a senior software engineer who has been developing business and scientific applications for 21 years. He has used code generation throughout his development work. In 2002 he started www.codegeneration.net to provide educational materials and support for code generation techniques in software development. He lives in Union City, California, with Lori, his wife of 10 years, and his baby daughter, Megan Michele. He is a proud Miami Hurricane.