The Generation of Code Generation
Code generation has existed for a very long time. It was originated by developers seeking to reduce the mundane, repetitive and boring aspects of application development, and today, just about every development platform and programming language can leverage code generation to some degree. In this series of blog posts, I plan on reviewing two primary topics:
- The various levels of code generation that exist today for Spring-based applications
- The ways that code generation increases developer productivity
While there are a variety of code generation solutions related to JEE and Spring development, this series of blog posts will focus primarily on the functionality provided by Skyway Builder.
Before diving into the various levels of code generation that are available for Spring-based web applications, I think it’s worthwhile to briefly review code generation in the context of JEE application development. The goal of this first blog post is to set the context for the rest of the series, and it isn’t intended to be a thorough review of the history of code generation for JAVA.
Code Generation and JEE Application Development
The software development industry has been tackling the complexity of software development for a very long time. While the early implementations of enterprise JAVA (JEE) introduced a standard programming model for web-based applications, it suffered from complexity and a requirement on excessive amounts of JAVA code. A JEE application consisted of both application-specific code (business logic) that was directly related to the functionality of the system being built, and also a lot of boilerplate code that was required by many of the JEE-related API’s. In many cases, the application code and boilerplate code were commingled, making JEE development even more difficult.
Dissatisfied with the state of JEE application development, architects and developers embarked on making things simpler. It was at this point that code generation really took off with JAVA. While developers were starting to experiment with using code generation for the business logic, the low-hanging fruit of JEE complexity was clearly in the generation of the boilerplate code. This is where technologies like XDoclet came into existence and thrived.
New Styles of Development Enabled by Code Generation
As the JAVA platform evolved, the JEE programming model also saw some major improvements. A concerted effort was made to simplify application development, and many of the complexities of JEE were targeted. While the platform already leveraged code generation in some places (i.e. JSP/Servlet), there were opportunities to expand the use of code generation to further simplify the JEE programming model. Annotations were introduced in Java 5.0 to generate a lot of the boilerplate code automatically. This declarative style of development shifted the burden of generating the boilerplate code; boilerplate code was generated by the compiler instead of the developer. In addition to providing many out-of-the-box annotations, Java 5.0 also provided an annotation framework for defining custom annotations. This standardization enabled third-parties to build custom annotations to harness the power of code generation.
With boilerplate code being managed and generated in a standard way using JAVA annotations, a greater portion of the JAVA code that remained was actually related to the functionality of the system. Additional reduction in the amount of boilerplate code was made possible by dependency injection, a mechanism for decoupling application components from lower-level infrastructure logic. As opposed to an application component being responsible for provisioning the components and services that it’s dependent upon, with dependency injection the dependencies of an application component are passed to it. While there are numerous benefits to dependency injection, a major benefit was the reduction in repetitive code required for provisioning dependent components. This led to a new style of developing enterprise JAVA applications referred to as Plain Old Java Objects (POJO). The POJO-based style of development made it easier to define and manage business logic because the business logic was less tied and less tangled in infrastructure code.
POJO-based development leveraging dependency injection and annotations is very powerful, however it wouldn’t be possible without implementations of each. As is usually the case with JAVA, various implementations exist. In contrast with the core services in JEE, there isn’t a JAVA specification or standard for dependency injection. There also isn’t a preset list of possible annotations. Therefore implementations can vary quite a bit in both conventions and functionality. The most successful implementations are the ones that are part of a comprehensive application framework. The Spring framework is one such framework, and it fully exploits the power of dependency injection and annotation-based development. For this reason many believe it has become the defacto standard JAVA framework.
Generating Fully Functional Applications
With the emphasis on simplicity and advances in JEE application development, some JEE practitioners feel that we’ve reached the limit of the practical use of code generation. It’s interesting that today’s criticisms and arguments regarding code generation are relatively the same as five or even ten years ago. Yet code generation continues to creep into JAVA/JEE development at a faster and more ambitious pace, making application development easier. Most recently several RAD platforms, including RAILS and GRAILS, have been further popularizing code generation through the use of scaffolding. Scaffolding, which will be covered in detail in a later post, consists of generating full applications by applying standard application patterns from a minimal set of inputs provided by the developer. Whether these scaffolded applications are used as-is or as the starting point for additional developer customization, scaffolding is very effective at jump-starting application development.
The Spectrum of Code Generation
Code generation is an enabler of many of the improvements and simplifications in JEE application development, and the application of code generation is very broad. At one end of the spectrum code generation is emanating from the lower levels of the application stack. Improvements to the JAVA language, the use of new architectural patterns like dependency injection and the capabilities enabled by modern JAVA framework implementations (i.e. Spring Framework) are all rooted in code generation. Cumulatively they have made application development easier by providing a foundation of code generation.
At the other end of the code generation spectrum there is scaffolding, the generation of fully functional applications through the automated instantiation of application patterns, like CRUD. Scaffolding has highlighted the potential for code generation beyond just reducing boilerplate code, and it represents a more course-grained application of code generation. As the formal specification of additional application patterns are derived, code generation technologies are prepared to automate the creation of systems based on those patterns. Coincidentally this would be a significant milestone for industrialized software development.
Whether you are using code generation at a fine-grained level or a course-grained level, code generation offers sustainable benefits in both productivity and quality, and the benefits apply to both application development and maintenance. We will continue to see refinement and innovation at both ends of the code generation spectrum. Furthermore with the applicability of code generation becoming more mainstream, there is an opportunity to expand on the conventional use of code generation to achieve even greater benefits. The essence of code generation is to achieve technical and functional objectives with minimal input and in as few steps as possible, and this can be applied to just about any aspect of application development.
In the next several blog posts I will be diving into the various aspects of code generation available for Spring MVC and Spring Web Flow applications using Skyway Builder. I will cover bootstrapping, scaffolding, application artifact generation, artifact implementation generation, and model-driven development. You will discover how you can leverage some or all of these code generation capabilities on your Spring-based application projects. I will describe how Skyway Builder applies conventional code generation techniques, and we’ll explore some of the innovations in Skyway Builder to further exploit code generation.
Let me know your thoughts. Does this seem like an accurate portrayal of code generation? Do you have any specific questions you’d like answered in this series?
Tags: Code Generation, java, Skyway Builder, spring








August 4th, 2009 at 1:14 pm
[...] my blog post “Generation of Code Generation” I discussed scaffolding in the context of code generation: “Code generation continues [...]