Code generation
-Avinash Atreya CustomerXPS
WTH? Generating code in a target language based on the declaration of a model / intent in a source language Example: XML declaration to a Java Bean Input can be XML, Excel sheet, GUI Widgets, DSL etc – something concise, fluent and natural to the problem domain Output is executable code in the target language – Java, SQL, HTML, etc.
So what’s the big deal
All the boilerplate code is taken care of, so the programmer can focus on the business logic There is one place to change stuff common to all business objects The same declaration can lead to multiple generated targets Generated code is predictable and consistent Declaration doesn’t need to change when some targets change Productivity – lines of code the developer has to write is far lesser than the generated code
Real world example
A simple system: Business objects that can be persisted and read from a database via a HTML based front end. <Entity name=“Customer”> –
– … From a single XML declaration we can generate – POJOs – JSPs for manipulation of POJOs – Hibernate mapping files for persistence
Example continued
New requirements – changes in objects trigger changes in other objects, we need jazzier front end in Flex Declaration doesn’t need to change at all, all that needs to change is in the code generator – in one place POJOs had only members, getters and setters to begin with. Now they also have Property Change Support, an additional computeX method like setX and getX for each attribute x Form handling in JSP remains the same, but the forms are rendered in Flex (different target) – which can again be generated
Implementation – take one The
basic idea is to do simple template processing in (say) *drumrolls* Python Read from source to Python data structures Use Python data structures to substitute parameters in the template and output the code Major flaw – no target abstraction, only templates: harder to change; no re-use
Take two – fix the target
High level target objects which hide away the template details Examples: Java Class, Hibernate Mapping, SQL Query etc Separate reading from the source, construction of the model (the data structures) and target generation (template processing) Target generator and source reader can be re-used for different models – they sort of form the code generation framework
Take three – fix the source (DSL)
The input for code generation is often Domain Specific Languages (DSL) – Using the syntax of a programming language – Custom syntax DSLs have all the characteristics of a good source language – Concise – Fluent (in either the domain / English) – Declarative XML is too verbose, spreadsheets are not great for hierarchical models (which is usually the case) Examples of DSLs: – JQuery (personal favorite), SQLAlchemy (python voodoo poster boy), DJango and Rails (frameworks, but contain DSLs), Camel for ActiveMQ message routing (DSL in *hold your breath* Java! )
Extension points Extend
the DSL to add new parameters, new
syntax Modify the code generator to add custom code, allow for hooks to do so Subclass the generated objects (when possible) to new functionality The first one is best in terms of re-use, and the last one in terms of ease and time
Utilities
DSL friendly languages – Lisp, Ruby, Python etc – – – –
Operator overloading and syntax extension Meta programming – meta classes and decorators Generative methods – easy to clone objects Keyword based arguments for extensibility, readability
Parser generators to parse DSL (custom syntax) – example ANTLR String template processing – Python and Ruby have good inbuilt support. StringTemplates, Velocity in Java, erb in Ruby are good choices.
More dope Lisp
Macros Meta classes and meta programming in Python GUI and IDE for the source – widgets and the ‘drag and drop’ stuff Don’t tell anyone I told you these – –
Domain Driven Design (DDD) Model Driven Architecture (MDA)
Resources http://codegeneration.net Manning’s
‘Code Generation in Action’ Brian Beck’s talk about Python and DSLs DJango’s SQLObject / SQLAlchemy source code in Python – for DSLs and creative use of meta programming (yes it is useful!)
Time to wake up folks
That’s it. Q & A