Code Generation in Action 262
Code Generation in Action | |
author | Jack Herrington |
pages | 342 (10 page index) |
publisher | Manning |
rating | 9 |
reviewer | Simon P. Chappell |
ISBN | 1930110979 |
summary | A masterful tome. |
Overview
Code Generation in Action, CGiA to its friends, is presented in two parts. The first part is four chapters, and covers a code generation case-study, the basic principles of code generation, including the different types of code generation strategies together with reasons why you would or would not use each strategy. The book's chosen toolset for building generators is presented next, and then some walk-through examples of building simple generators wraps up the first part.The second part is a kind of a cross between a cookbook and a list of engineering solutions. There are nine chapters with the breadth of solutions covered being quite impressive, covering the gamut of generation of user interfaces, documentation, unit tests and data access code. Each chapter presents a couple of solutions within its topic area, often for different technologies within that topic. For example, the user interface chapter covers the generation of Java ServerPages, Swing dialog boxes and then Microsoft MFC dialog boxes. No favouritism here!
What's To Like
There's a lot to like with this book. The writing is very clear and of good prose. I found the introduction to be very compelling, and I felt completely drawn in by the opening case-study. The four chapters of part one are a concise case for code generation, and would be very useful information to help persuade co-workers and management of the positive risk/benefit ratio with trying code-generation on a live project.It would be impossible to try enough of any solution from part two in a time-frame short enough to make this review useful, but in the solutions that match my areas of knowledge, I found myself admiring Herrington's straight-forward and pragmatic approach.
What's To Consider
There are two aspects of this book that I want to flag. One of these aspects, some will love and others will hate, and that is the choice of generator language for CGiA. The author has chosen to use Ruby as his working language. This is an interesting choice. Ruby is certainly a language that is inspiring a lot of admiration these days (in fact, it's hard to get Dave Thomas to stop talking about it :-), but with the majority of the code-generation examples being for Java-related technologies, I wonder why Java was not selected instead.I also found myself wondering about the lack of discussion of how to integrate these Ruby tools into a typical Java build process. Many developers I know use ant to bring automation and consistency to their builds, yet the book doesn't mention this. (JRuby anyone?) Certainly something to consider for the second edition or future code-generation authors.
SummaryThis is a masterful tome that inspires and delights, although the two issues raised above did cost it a perfect score of ten.
Table Of Contents
- Code generation fundamentals
- Overview
- Code generation basics
- Code generation tools
- Building simple generators
- Code generation solutions
- Generating user interfaces
- Generating documentation
- Generating unit tests
- Embedding SQL with generators
- Handling data
- Creating database access generators
- Generating web services layers
- Generating business logic
- More generator ideas
You can purchase Code Generation in Action from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
I belong to the Code Generation... (Score:5, Funny)
Richard Hell and Codeoids
good stuff (Score:5, Interesting)
My personal favorite trick at the moment is describing interfaces in XML, and getting multi-language bindings and docs out of it with a few XSLT scripts. Techniques like that makes script language bindings easy as pie (as do other systems like Swig).
Re:good stuff (Score:5, Funny)
Ya, I've done some code generation myself. I used a text editor and my brain. How does everyone else do it?
I thought about using my wang instead of my brain but he just types screenfulls of the V word.
Re:good stuff (Score:5, Funny)
I thought about using my wang instead of my brain but he just types screenfulls of the V word.
" V", huh? I feel sorry for your romantic partners. My wang can type " VFR"...
" VFR4" if I'm really excited.
Re:good stuff (Score:3, Insightful)
We farmed out a project to a company which used a ton of "elite" off-shore resources, and they sent back a project which relied heavily on XSLT. Granted it made sense on paper -- prior to their involvement, the data was already available in XML format. But the net result was a nightmare to debug, maintain, and upgrade. XSLT reminds me of the old saying about APL -- it's a "write-only" language.
Ok, I concede it's not actually as bad as APL, but it isn't nearly as easy to debug as r
Re:good stuff (Score:2)
It probably wouldn't be bad at all to just use a DOM-based parser in Perl or Python or something and use that to generate the output.
Re:good stuff (Score:2)
This has been too long of a day. Now I'm thinking about "DOOM-based parsers". Kind of like that old "sysadmin DOOM hack" where you could take out processes with a shotgun, but for code...
Re:good stuff (Score:2)
Maybe, maybe not. I have used code generation to great effect, by making tiny C files for each of the database tables that provide c structs and simple operations such as insert .
Why am I doing this? Because there's no high level interface between C and the database. Code generation is often needed because two system just can't mesh comfortably. This is a fault in both systems.
Let me illustrate with a quote
Re:good stuff (Score:2)
Created a crude domain specific language where display control and data type are contained in a hungarian-style prefix of a string that is the field's name.
Had some control information (are we updating/inserting, what table, key field for updates, how many SQL statements) in another string that went into a hidden field.
Result: a library that transformed the Request.Form collection in one pass into an arbitrary number of SQL statements against arbitrary
Is code generation always necessary? (Score:2)
Software Engineering is not just there yet (Score:2, Interesting)
Re:Software Engineering is not just there yet (Score:2, Insightful)
Re:Software Engineering is not just there yet (Score:5, Informative)
* Templating (see Alexandrescu's book on Modern C++ design)
* Macros for those in the Scheme/Lisp world (these are GREAT and AWESOME)
* Compile-time programming (only available in LISP as far as I'm aware through the eval-when construct)
* Custom program-generators
And then there's the related concept of partial evaluation that, while excellent, has received very little attention by the commercial sector.
Now, many code-generation facilities could be done better with good libraries, but this isn't universally the case. Delphi is probably the best at putting in libraries/properties what others put in code generators, and their software is much easier and better because of it.
Macros and compile-time programming are two of the best ways to do this, but Scheme and LISP are the only ones that do this reasonably.
Re:Software Engineering is not just there yet (Score:3, Insightful)
Re:Software Engineering is not just there yet (Score:2)
(say (combine (exclaim Gosh!) (active-clause (subject I) (verb (auxilliary (not can) think)))))
cout << "I can.\n";
System.out.println(":-)");
Re:Software Engineering is not just there yet (Score:2)
Also possible with template meta-programming in C++, which is one of the reasons recent expression template libraries and their ilk are getting such good performance.
Re:Software Engineering is not just there yet (Score:3, Interesting)
For example, many games use precomputed lookup tables. It would be nice if the entire table could be generated at compile-time. You might be able to do it with a really awkward template, but it's best done using straight compile-time programming.
What's funny about C++ template metaprogramming
Oh yes it is, it's just not widely accepted yet (Score:2)
I've spent the past 7-10 years working with different code generation approaches. My latest R&D efforts meld a variety of the techniques I'd used in previous efforts to provide a code generation engine that has proven flexible enough to produce RDBMS schema scripts, C++, Java, etc.
Even better, I've been able to work towards a higher level abstraction which allows me to change the architectural "glue" while maintaining a consistant application developer API. (i.e. Choose amongst different RDBMS serv
Something missing? Like a definition? (Score:5, Insightful)
Why not put a little bit about code generation in the review. Even a little blurb like "It builds the mundane portions of coding" would have helped out a bit.
Re:Something missing? Like a definition? (Score:2)
Eh? This is different than I would expect. In the "formal" computer science definition would be something like generating code (often times machine code or byte code) in compiler context.
Also, in computer software design, it has another definition. Namely: Generating real code (C, C++, Java, whatever) from design templates (like from UML).
Now, these people are overloading the same term?? These make me even more confused.
Re:Something missing? Like a definition? (Score:5, Insightful)
[rant]
What the hell is up with these book reviews? I equate book reviews with SCO. All I see is a large body of reviewers making unsubstantiated claims to a book that tickled their fancy in a personal sort of way. They say basically, "this book tickled a nerve, but I will not say what I already know that led to that nerve being in place already." Of course it turns out then to be some haphazard therapy session where the reviewer begins to delve into themselves while completely ignoring their audience!!!
OK, I know /. may not be extremely high with the English majors, but how about we don't post such things submitted by such arrogant posters? (is it possible?) I don't claim myself as being a master of prose, but you won't see me trying to do something I knowingly can't.
One more point, the reviews most commonly given are little more then amazonian [www.amazon] reviews. They rave about how great something is, realize that only constitutes of 2 senteces, and proceed with immediately filling in the blanks with worthless prose to create content.
And lastly, I don't receive book reviews on my main page because of their vapid nature. This means that /. is losing on my potential business. I would love to see them prosper, but they have to create something that is interesting. Slashdot is all about bringing the obscure to the masses, but I hardly see that through their book reviews. What is the freakin deal with OReilly's cookbooks? Are there any really, really bad OReilly books at all? We all have hordes of them or wish we had complete collections. We know what a cookbook is and they don't really differ that much between subject.
I am trying to prove a point here, that reputation and common sense through the title allow for little differentiation between your preconceived notions about the book and what the book is realistically about. I want books to come to the forefront from the dirges, such as the recent classic The Elegant Universe: Superstrings, Hidden Dimensions, and the Quest for the Ultimate Theory [amazon.com] (Feb 2000) by Brian Greene, or "been around the block more then once" Men of Mathematics [amazon.com] (June 1937) by Eric Temple Bell. My point being, either book is known to those in the "know," but is rare to be known by budding scientists (high school students) and younger folk. I won't let this branch off into a rant in poor public schooling and child upbringing. I only want to see objective viewpoints of why the book may be helpful to me, not why it made you change your perspective on life! FSCK!
[/rant]
Thank you for being patient.
Indian Consultants and Code Generation (Score:2, Funny)
Code generation == metaprogramming (Score:5, Insightful)
Having 1 language generating another - Ruby and Java - is the recipe for confusion and complexity.
Re:Code generation == metaprogramming (Score:5, Funny)
Yeah, honestly, these upstart newbies yaccing on about code generation as if it were some sort of new idea. Lex think for a minute and bison other books before we get carried away, shall we?
(And "active code generation" used to be called "high-level languages" and used fairly often by stronger programmers who didn't like cut-and-paste. Maybe that's not buzzwordy enough any more, though.)
Re:Code generation == metaprogramming (Score:4, Insightful)
Re:Code generation == metaprogramming (Score:2)
Yep. Just take a look at C++: the "regular" language and templating language are very dissimilar, making it very difficult for someone to understand someone else's "clever" template.
Re:Yea. Ok. Perl do it, too. (Score:5, Informative)
The point your parent poster was making, is that a lisp program has the full power of the language available at macro expansion time, just prior to compilation. This means you can redefine the syntax of the language at will to create any language you like on top of lisp. Lisp macros should not be confused with c-style macros, which are merely token substitutions, not redefinitions of language grammar.
You only have this in perl in a very crude and hackish sense. You have to write your own parser; you have to write your own code generator; you have to run every piece of code through your home-brew parser/code-generator before you send it to the perl interpreter. Debugging? Heaven help you if any of:
1. Your code written in your new-language-built-on-perl has errors
2. Your parser has errors.
3. Your code generator has errors.
In Lisp, the macro facilities come for free, are part of the standard (so macros are portable). Vendors are responsible for correctness, so debugging is a simple matter of using the built in functions macroexpand and macroexpand-1.
Saying that Perl has the same sort of code generation capabilities as Lisp is rather like saying that, since all languages are Turing equivalent, assembly language has the same macro capabilities as lisp.
The power of a language comes from its expressiveness, the things it lets you do easily without having to resort the 21st century equivalents of a turing machine. With Perl, you only get this level of expressiveness by using convoluted, error prone, home-brew substitutes for real macros.
Re:Yea. Ok. Perl do it, too. (Score:3)
I have done a lot of it (and I don't think it has every been generating perl code). Mostly been for "code" in the larger sense. See Pragmatic Programmer for details.
I don't know lisp, so I don't know if it would be better,but your statements are not correct. you don't have to write a prse nor a language generator.
we tossed together a very simple system for Kickstart file generation (automation scripts for RedHat's anaconda installeD). basically
Re:Yea. Ok. Perl do it, too. (Score:2)
There is a lot more going on here [moderncppdesign.com] (for instance) than text-substitution. The syntax for C++ templates may make LISP's seem downright pleasant, even for a LISP-hater, and some of the techniques border on language abuse, but it forms a full-blown compile-time functional programming language that integrates fulling into the C++ type and class mechanism. The Boost library collection [boost.org] is full of such metaprogramming, such as
Wild (Score:3, Funny)
They make the visual of some pale, skinny nerd with a "Got Root?" shirt on, banging away on a keyboard in a poorly lit dorm room sound exciting.
Re:Wild (Score:3, Funny)
Re:Congratulations (Score:3, Funny)
We didn't know we were doing anything special... (Score:5, Insightful)
My rule of thumb is if I find myself writing code that bores me silly and thinking "a frigging monkey could be taught to code this piece", I will strongly consider writing a program to write the program for me. Be warned about the maintenance and readability issues though in larger development projects where there are a lot of mediocre programmers around. You can always assign those mediocre programmers to hack on monkey-easy code, but you can't get them to hack on a code generator, so carefully consider the nature of the development organization you are dealing with, and the tradeoff between available "high-value" time and resources vs. "low-value" (i.e. monkey coder) time and resources. This perspective has been brought to you by my pointy-haired side.
Re:We didn't know we were doing anything special.. (Score:2, Informative)
Re:We didn't know we were doing anything special.. (Score:3, Interesting)
Re:We didn't know we were doing anything special.. (Score:2)
Instead, XDoclet should let you speci
Re:We didn't know we were doing anything special.. (Score:2)
First generation (Score:2)
If the complexity of your generator is too much for people to maintain, it sounds like you're still working with ad-hoc hard-coded generators. It's helpful, but as you say, it creates a whole new set of code to maintain.
There are better approaches.
Code generation and Pragmatic programmer (Score:4, Informative)
But, even here, I think the UI pages, or template based generators definitely help. There was a tool by DevelopMentor which used to generate code for ATL. It was based on templates..
Re:OO/Inheritance doesn't eliminate the need (Score:2)
You mean OO isn't a silver bullet, and generics might be useful after all?! Sacrilege!!!
NOT about compiler code generators (Score:5, Interesting)
But apparently, it is not. I, for one, wasn't too happy seeing the term "code generator" applied to superficial software that generates HTML/user level code for standard dialog boxes etc. HTML isn't code in the first place, anyway.
Maybe I'm being too fussy about this, but a code generator, traditionally has always meant a part of the compiler back-end which actually translates intermediate code to machine-level instructions. VB and other UI tools generate stubs for the most part...well maybe they are code generators, but I'm just not too happy about the choice of terms.
Re:NOT about compiler code generators (Score:2)
It's about bringing the power of #include and #define to other environments, as well.
Remember, programs that write programs are the happiest programs in the world.
Re:NOT about compiler code generators (Score:3, Insightful)
I wrote a code generator for EJBs. The template that was passed in was very much the source as a very high level language. The output was Java code.
I wrote a Pascal compiler for a virutal machine. The source taken in was Pascal, the output was VM code.
The two were very similar except the gramar for the code generator was much simpler so I didn't need a complex lexical parser.
Re:NOT about compiler code generators (Score:4, Insightful)
I think you're being too fussy. Anything that generates code can reasonably be called a code generator. You are just most familiar with the backends of compilers.
Code generation can allow a developer to compensate for missing abstractions in the underlying language or architecture. For example, it's almost trivial to write generator for a Java type-safe enum class [sun.com]with a couple of pages of java tied to a Velocity [apache.org] template for an enum. You just have to be sure that your build process regenerates the class if your input changes. The input could be something as simple as
color {red, green, blue}
It's then easy to add features to your enum infrastructure that aren't in Bloch's class and have them show up in all the project enums (like correct serialization and localization).
The GUI builders that spit out code fragments are just the tip of the iceberg when it comes to code generators. Imagine a utility that could generate the plumbing to make EJB or RMI methods directly accessable from a Windows DCOM client, without it knowing or caring that it's talking to Java on the other end.
Or automatically generating classes for Customer, Order, and Product from an existing database table, and seamlessly loading, caching and saving their instance data without the application knowing or caring that it is persistent.
Automating this sort of tedious programming is what code generation is all about.
Yes: it's about covering weaknesses (Score:3, Insightful)
Thank you; that was the most insightful comment I've seen here all day.
Code generation, like design patterns and such other trendy things, is just a technique you can use with weaker languages or designs to gain some of the power of stronger ones, if you don't have the option to use something more expressive directly. As such, it merits serious consideration as a tool in the toolbox,
Re:NOT about compiler code generators (Score:3, Insightful)
I remember when Microsoft was launching all of their visual programming products. Visual programming purists complained (correctly) that these products had nothing to do with visual programming- they were just IDE's that included visual form designers.
The more obscure definition is going to lose. But guess what- it doesn't really matter.
Like Juliet said, "What's in a name? That which we call a rose/By any other word would smell as sweet."
Re:NOT about compiler code generators (Score:3, Insightful)
Why ruby to generate java code. (Score:2)
The other advantage using one language to generate another is that there is less confusion about what code is part of what. In my case I had ruby in the generator and java and MS sql in the text blocks that were being generated so it was easy tell thing apart.
Am I FUD? (Score:2, Insightful)
If you are able to clearly separate your code in
Re:Am I FUD? (Score:2)
Code Generation is for people who don't understand or are too lazy for abstraction, and it will ALWAYS have the problem of, what if you want to go through all your projects and change one single thing about the generated part of your code?
While I completely agree with your statement, I don't think it wholly applies to cases where you are using code generators to do things like generate an XML config file, or other such cases where you are using different technologies that don't talk to each other well. X
Re:Am I FUD? (Score:3, Insightful)
The article that timothy suggested as background reading ( here [devx.com]) points out that code generation is most useful when you're forced to use a framework that requires lots of simple-minded "scaffolding-style" code. EJB is the prime example.
In other words, I agree with you --- if code generation is useful, it's probably because the infrastructure you're using was poorly designed. But that doesn't mean you don't have to u
Re:Am I FUD? (Score:2, Interesting)
Re:Am I FUD? (Score:5, Insightful)
While I agree with you in principle - that better abstraction is usually the way to go - that's not always possible in the real world. For example, sometimes you have to produce an API for someone else, or hook up to their API. In those cases, better abstraction isn't always an option. Sometimes your boss says "you're using EJB" and you're stuck with it. In those cases a generator can be a big help.
The cool thing about integrating a generator into your build is that you get the benefits of abstraction without many of the drawbacks. The generator becomes your abstraction, so you can make modifications in one place. Sure, using a generator requires a little more thought than cut-n-paste. So does proper abstraction. The two aren't all that different, and both approaches have their place.
Re:Am I FUD? (Score:3, Interesting)
Re:Am I FUD? (Score:2)
--jeff++
Not FUD, but not correct, either. (Score:5, Insightful)
Code generation is a practical, efficient tool for solving many problems where OO-style abstraction need not enter the picture. One such class of problems is building interfaces and glue code from external specifications.
A few years ago, I wrote a simple code generator that reads the SQL DDL for a large database and generates an object-based interface to the database. Client coders could then use the object-based interface to access the database. The advantages of this approach proved to be numerous:
Descriptive code vs. code generation (Score:2)
But I'm not very clear on the advantages of code generation versus descriptive code. One example I like to use is glade versus libglade. The ad
Re:Am I FUD? (Score:2)
Re:Am I FUD? (Score:2)
Seems like 6 dozen of one, and 72 of another...
Re:Am I FUD? (Score:3, Informative)
I take it you've never used a compiled language? In an abstract sense, that is a code generation. Actually, so are interpreted languages: you give them a high level expression and it turns it into executable code that it runs for you instead. Have you ever set up a
Code Generation in Java... (Score:2)
It's also very tightly integrated with ant [apache.org] and maven [apache.org], the two build tools that are used in just about every project I'm involved in.
You can find out more about it here [sourceforge.net].
Ruby not Java (Score:5, Informative)
I think the author makes it pretty clear why he chose Ruby instead of Java. Essentially, in order to parse text (which is one of the primary functions in code generators) you would have to write 2 to 3X more code in Java than you would in Ruby. Java is not an optimal text parsing language - first off you have to find a regex engine for it. That leaves you with choosing one of the scripting languages: Ruby, Perl or Python.
Here's what the author says about the cons of using Java for code generation (page 41):
* Java is not ideal for text parsing. (I would agree)
* Strong typing is not ideal for text processing application (again, I would tend to agree, strong typing only gets in your way)
* The implementation overhead is large for small generators. (you'll be writing a lot more java code than you would in Ruby to get the same thing done)
Overall, I'm finding it to be a great book, and the use of Ruby for implementing the examples is a plus as far as I'm concerned.
As far as integrating Ruby into the build process goes, I recall hearing something about a project that uses Ruby to drive Ant.
Re:Ruby not Java (Score:2)
Re:Ruby not Java (Score:2)
Like, for instance, the built-in java.util.regex package?
Re:Ruby not Java (Score:2)
Why is it that when the term "scripting language" is brought up, PHP is so universally forgotten?
It's excellent at text parsing, (which is its native purpose) widely supported, and incredibly flexible.
Re:Ruby not Java (Score:3, Insightful)
That makes it great for small dynamic sites (which it is frequently and effectively used for) and crap for everything else.
Re:Ruby not Java (Score:2, Informative)
-austin
Re:Ruby not Java (Score:2)
Good subject, bad example (Score:3, Interesting)
"Even if code generation could build 100 percent of the application, there will still be an endless supply of boring meetings about feature design."
----
" For example, when a lot of code needs to be written to perform relatively simple tasks, engineers can smell bad design. The EJB platform requires the construction of a bunch of classes and interfaces for each database table. Some consider this a design smell."
----
Wow. So I have boring meetings and a really bad platform to look forward to. Yowza, bring it on!
OK, I'm partially joking here. But I think it's worth considering... when we're glad that we have a tool to alleviate the hassles of using a technology (like EJBs), maybe the technology and platform needs to be reconsidered? Shouldn't the platform perhaps do more to "hide" the complexity from us?
I'm struggling with this in a similar form right now with SOAP. I'm deploying a set of SOAP services in Java using Apache AXIS [apache.org] implementation. It does a lot of the 'heavy lifting' for you, but I'm still struggling with the particulars of how to deploy a service, serialize/deserialize my classes, ensure this API works well across a variety of clients (I'm testing with Perl, PHP, and Java), etc.
It can be a bit daunting. But... simple things *are* simple, and the level of code generation available is just about right. I don't *have* to generate a bunch of "helper" classes to put a SOAP service out there, the Axis implementation hides most of those details. I *can* generate additonal classes, and customize my WSDD and WSDL files to handle more complex situations, though. So, it's not perfect, but maybe it hits the mark a little closer.
Anyway, code generation can be great. I've done it before with things like interface creation (take an interface description and go to either HTML or Swing), and it can be very handy. But I wouldn't want to implement a system that is unusuable as a platform without those tools. Real programmers use vi
not impressed. (Score:3, Insightful)
Huh? Software engineers use more software than anyone else. We have tools for our tools. I found the above statement bordering on the ludicrous, and almost stopped reading at this point.
Code that is copied and pasted to multiple places is difficult to maintain properly across all of the copies. Active code generation does not suffer from the same maintainability issues as copy-and-paste coding. When you need fix something, you apply the bug fix to the templates used to generate the code, which then propagates the fix to all of the code maintained by the generator. This design ensures that no code that needs fixing is left scattered around and forgotten.
That's why we use functions and classes. Then, when you change your function, the changes are magically propagated to all the places in your code where that function was called! Copy and paste programming has been frowned upon pretty much since the days when the goto was declared bad programming practice.
It really sounds like this book is just putting on a fancy name for an incomplete set of good programming practices. Really, what is covered here that Design Patterns doesn't cover in a much more thorough and professional way?
Re:not impressed. (Score:2)
I'll suggest that they probably meant copy-paste-modify rather than just copy-paste. Very similar code is necessary in places like the db abstraction and object mapping layers for db backended apps and while the code is very very similar (ie, every single object needs to know how to add/update/remove/retrieve itself from the database), the individual elements and table layouts are di
Re:not impressed. (Score:2)
That's true, but functions and classes don't express all, or even most, of the patterns that are present in our applications.
See "Aspect Oriented Programming" (google for it) for a set of extensions to some common languages. By seeing the problems they solve, you should get a better idea of what things functions and classes can, and cannot, solve.
-Billy
Code generators are just compilers... (Score:4, Interesting)
I became a code-generation convert about two years ago when I first encountered Perl Data Language -- what made PDL development feasible was a code generator by Lukka, Glazebrook, and Soeller: their generator ("PP") writes all the loops and conditionals that do vectorized processing of large arrays.
Of course, PP has its limitations -- the age old complaint of engineers is that they will eventually outgrow any code generation framework -- but it's simple enough to augment the generation language or, in extreme cases, to sidestep the parts of the functionality that you're not using.
Re:Code generators are just compilers... (Score:2)
Re: (Score:2, Informative)
Been doing this for YEARS (Score:2)
There are usually two cases:
1. The need to automate the generation of some type of code or script for a large number of test cases, where the scripts would otherwise be generated manually.
2. The need to generate tests to excercise implementations from what can best be described as a machine readable description of interfaces and exchanged data types.
The second case is rather interesting in that all the information in most
Code generation a necessity (Score:4, Insightful)
Re:Code generation a necessity (Score:4, Insightful)
To me it manifests itself in the ass-first design:
if I work with a framework, the typical scenario for OOP would be: framework provides interface, I implement it with my classes so they can play in the framework.
With EJBs, it is another way around: you define an interface and the container/framework creates classes and implements your interface for you!
Code generaton became so popular with J2EE for this very reason: there is _always_ a step where you need to produce a lot of redundant EJB artifacts so you might as well automate it.
Re:Code generation a necessity (Score:2)
You know, this is a good point. I've done my fair share of EJB programming, and now I'm using the Torque [apache.org] framework as an object management layer. Torque will generate your business objects from an XML database schema. It _is_ nice, but it's basically the same class with different getter/setter methods. Perhaps there is a better way to do it.
If you're going to use big words... (Score:3, Informative)
From http://www.hyperdictionary.com/dictionary/ambival
1. [adj] uncertain or unable to decide about what course to follow; "was ambivalent about having children"
2. [adj] characterized by a mixture of opposite feelings or attitudes; "she felt ambivalent about his proposal"; "an ambivalent position on rent control"
Is that really what anyone would expect from a reference material? I think the poster wanted "...no pretense of objectivity."
Not that I care that much, of course.
Cheers
-b
Swings and roundabouts (Score:5, Insightful)
I worked at a telco where we generated C code on the fly from high level Structured Definition Language [etsi.org] for the main call control processing.
It was a great idea... in theory. In theory, it was impossible for the implementation to get out of sync with the detailed design (the SDL). In theory, there's no difference between theory and practice, but in practice there is. Some of the features that we had to add simply couldn't be modelled in SDL, plus there were performance issues, and it produced ugly source.
It was used for fifteen years (yeah, pre-ANSI), but it eventually collapsed under the weight of all of the hacks that were required to work around the limitations. We eventually had to admit that the behaviour of the complete source (generated plus all the stuff around it) was now so different from that defined by the SDL that it was no longer worth putting up with the limitations of the SDL.
In the end, we just took a snapshot of the generated code and set developers free to actually fix it rather than hack around it. At that point, there were only a few people left who even knew SDL, so there were very few tears shed. The rest of us cheered, and the product got significantly cleaner as we refactored the bejeesus out of all the generated C and removed the hacks.
I'd recommend giving code generation a try, but don't be ruled by it. Once the product is mature, if the code generation is limiting you, then don't be afraid to drop it and fix the lower level generated code.
code generation v. decent languages (Score:2)
Most of the examples of code generation I've seen around are basically kludges around insufficiently powerful high-level programming languages. Datatypes and representations should be able to be generic in the programming language; this enables directly coding at the desired level of abstraction. Check out ML or Haskell for one cut on these ideas.
That said, I've been doing CG for a long time :-). The M4 macro preprocessor is a fine tool for this task. See e.g. my student's M4 CG for XCB [pdx.edu]: a classic exam
um. (Score:2)
Try writing it in Java, and I'll wager that you'll find out why. It'd probably be quicker to write a code generator to write it for you. Use the right tool for the job.
Integrating Java and Ruby (Score:2)
Just a heads up about integrating Java and Ruby:
There is a tool called rjni (Ruby JNI), from the README:
"What is rjni?
rjni exposes the Java Native Interface to Ruby. This allows the programmer to instantiate Java objects, manipulate them, def
Re:Integrating Java and Ruby (Score:2)
I thought J2EE was supposed to simplify things (Score:3, Insightful)
J2EE technology and its component based model simplifies enterprise development and deployment.
But now I hear that we need code generation to keep up with all the mundane tasks made necessary by the use of EJBs.
So we build a code generator and we have to maintain that.
This is on top of all the J2EE design patterns you are supposed to do because the world would come to an end if you just accessed a database table using JDBC directly.
Once in a while someone should look at the assertion that it would be harder to maintain a lot of imbedded JDBC code in your application than it would be maintain the 5 or so classes you need for each business object in order to maintain architectural purity.
Actually, a very interesting topic (Score:5, Informative)
(generic) C++ -> (specific) asm -> executable bits
(obviuosly, the C/C++ compiler doesn't need to generate asm, but it's still code generation if it does)
Code generators just take this a level higher, so the code "life cycle" looks like this:
(generic) Diagram / CG description -> (specific implementation) C++ -> (specific machine) asm -> machine code.
Code generators have a great potential for easing coding and documentation. Just like GCC has many backends to generate code for different processor architectures, the code compilers can have different backends to make source code in different languages (C, C++, Fortran, whatever). Even better, you can run a different translator and get documentation out of the "source" - in HTML, DocBook, XML, or any other format you want.
There are tools to let you make UML diagrams (Google for "Executable UML" for great goodness), and generate real-time C code for an application, a C++ app simulator that runs on a PC, and documentation for the system, all from the same diagram. The tools are expensive (like $15k-$30k), but for large projects, they can be a great savings.
I saw a program called BridgePoint (from Project Technologies [projtech.com]), which was able to generate embedded, real-time code that was as efficient (more in some areas, less in others, but it averaged out the same) as hand-optimized code done by expert programmers. It all depends on how goo dyour translator is (and this program lets you write your own).
Some books on the subject:
"Executable UML: A Foundation for Model-Driven Architecture", by Stephen J. Mellor and Marc J. Balcer
"Executable UML: The Models are the Code", by Leon Starr
"Real-Time UML: Developing Efficient Objects for Embedded Systems, Second Edition", by Bruce Powel Douglass
true story (Score:3, Interesting)
"Hey, this guy figure out a way to increase productivty to a point we can let three programmers go!"
"Well then, lets be sure one of them is the guy who figures out how to save us money!"
Re:true story (Score:2)
Gawd, that sucks.
Can you share any details?
What about expressing concepts at a higher level? (Score:2)
Generating a lot of repetitive code can be done by the computer, but can't that be taken care of at a layer that the end user need not worry about -- by defining the higher level concepts at the language level?
Here is an example: In Perl, there is a module called Class::ObjectTemplate. It is a template that can be used to define classes. A code can work as follows:
package MyClass;
use Class:ObjectTemplate;
require exporter;
@ISA = ("Class::ObjectTemplate");
attributes("a", "b", "c", "
Code Generation incorporated in .NET (Score:3, Interesting)
What is great is how they abstracted out the target language. Once you build a CodeDOM graph in your generator, you can pass it to any language generator (C#, VB.NET, J#, write your own) to create the result.
carefully now... (Score:2)
yer liable ta start a fight with that kinda talkin.
Is code generation always necessary? (Score:2)
My question is, wouldn't developers rather use purely object-based packages, if they could, rather than code generators? I believe purely object-based packages could work for most Object/relational tasks and simple compilers. The code becomes more portable and maintainable and doesn't require the extra training for use of the code gen
Oh ye lex and yacc... (Score:3, Informative)
When I first learned lex and yacc, I got tempted to turn every single useful C/C++ library into a scripting language. "Think how much time I could save!" I thought.
Well, while I still think developing an application specific language (to basicially make pseduocode functional) is an occasionally useful technique, what I found was that it usually made project transfer and maintenance more difficult and more expensive.
Using XML and XSLT to do the same thing as lex and yacc doesn't inherently add much. The exception would be the evolution of an industry standard DTD for, for example, common UI constructs. I can see value there. But rolling your own metaprogrammer strikes me as rarely of real benefit. The metaprogram becomes another thing that must be documented, explained, maintained, and transitioned. It adds something that may not be easy to integrate into a present or future automated build process.
I guess I'm coming down firmly on both sides here. My point is that the cost/benefit analysis for a single developer doesn't necessarily align well with the cost/benefit analysis for the project as a whole. I think we have all seen projects that are in "tool and library hell" where developers have included their favorite libraries and tools willy-nilly (a technical term I like very much -- so concrete, so precise) so that no one can actually get the project to build. The GnuCash project was like that for the longest time (and it is still a bit messy if you ask me).
Faster isn't always better or cheaper.
In other words, I have seen metaprogramming do more harm than good in my experience. And the few successes come when the metaprogrammed portion was well analyzed and understood, and a standard could be made that would apply to an entire enterprise and not merely to a single project. More often than not, the inclusion of metaprogramming became the first reason to rewrite an application -- no one wanted to figure out or maintain the metaprogram. So they chucked it.
Code generation and macros (Score:3, Interesting)
In theory, good programmers never write repetitive code: they factor out functions, classes. Still, there are things that cannot be factored out easily, patterns of code that just do not map well into functions or cannot be abstracted away in whatever way the language provides.
Some languages are better at abstraction than others, but not many provides the ability to generate code (think of syntactic abstraction). This is where Lisp macros shine. They can hide ugliness I'm forced to do in the name of performance, define new control structures,
Think of the number times you were forced to go out of your way to solve a problem because while it was more correct or efficient it was less easy on the eyes. True macros would have helped. Well, divide that number by 10, the rest was probably your fault
A language needing a code generator for reasons above lacks power and for this lack a thousand small tools will appear all trying to plug some holes.
Been there for some time now :-) (Score:2)
Code Generator, n. (Score:2)
Good and Bad (Score:2)
But code generation can be a sign that the wrong language is being used. The author recognizes this, but points out that
Best code generation tool (Score:3, Informative)
Yes, I'm biased, I use it extensively. Extensively.
Write your metamodel in XML, build code generation scripts, generate anything from interfaces to database layers to entire applications.
I took some of the examples from CGIA (which is an excellent book, I read it and I like it and I recommend it heartily) and converted them to GSL - simpler, clearer, more obvious.
If you are a professional programmer you need code generation. This is simply a basic technique, like editing text with a visual editor and not edlin. And of all the code generation tools out there, GSL is by far the most flexible and powerful, mainly because it was designed from the ground up, and has been used and evolved over about 10 years specifically as a code generation tool (unlike XSLT which does the job but with more weight and less elegance).
In my journal, I include a GSL script that generates a complete C interface layer for MySQL, turning a simple description like this:
<table name = "history" description = "Message History" >
Holds all messages received and sent. The command and body are parsed
from the smstext.
<field name = "id" domain = "recordid" >Record id</field>
<field name = "groupid" domain = "id" >Parent group</field>
<field name = "userid" domain = "id" >Parent user to/from</field>
<field name = "incoming" domain = "boolean" >Incoming message?</field>
<field name = "appl" domain = "msisdn" >Application MSISDN</field>
<field name = "text" domain = "smstext" >Message text contents</field>
<field domain = "audit"
</table>
Into a complete abstract interface.
Whatever: code generation is a cult technique that deserves a place at the center of every serious developer's toolbox, and this book is possibly the first one that I've seent that may achieve this.
Enjoy.
Re:Why?! (Score:2)
d[i]=reader(i);
}
I think that's what he's referring to. Why does everything have to be bound at compile time?
I've struggled with that same question many times, i.e. Hibernate or XDoclet?