Slashdot Mirror


Code Generation in Action

Simon P. Chappell writes "Now, I enjoy a good technical book more than the next geek, but it's been quite a while since one left me quite so excited with the possibilities that it presented. Code Generation in Action is beyond interesting, it is a masterful tome on its subject matter, written by one who is obviously an experienced practicioner in his craft." If "code generation" isn't a familiar term to you, this enthusiastic overview on devx.com is a concise introduction to what code generation is about, though it makes no pretense of ambivalence about its importance as a programming tool. Read on for the rest of Chappell's review. 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.

Summary

This 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
  1. Code generation fundamentals
    1. Overview
    2. Code generation basics
    3. Code generation tools
    4. Building simple generators
  2. Code generation solutions
    1. Generating user interfaces
    2. Generating documentation
    3. Generating unit tests
    4. Embedding SQL with generators
    5. Handling data
    6. Creating database access generators
    7. Generating web services layers
    8. Generating business logic
    9. 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.

262 comments

  1. I belong to the Code Generation... by burgburgburg · · Score: 4, Funny
    And I can take it or leave it if I please.

    Richard Hell and Codeoids

    1. Re:I belong to the Code Generation... by gowen · · Score: 1

      Not really Richard Hell, since "blank" -- the word you've changed -- was his only contribution to that lyric. The rest goes back to Rod McKuen (or "the woeful Rod McKuen" to give him his full title), who wrote "I Belong To The Beat Generation"

      --
      Athletic Scholarships to universities make as much sense as academic scholarships to sports teams.
  2. good stuff by DreadSpoon · · Score: 4, Interesting

    Code generation is definitely something programmers of large/complex projects should look into. There's a lot of different forms of it, and I'd be surprised if people haven't used on form or another already.

    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).

    1. Re:good stuff by SpamJunkie · · Score: 5, Funny

      Code generation is definitely something programmers of large/complex projects should look into. There's a lot of different forms of it, and I'd be surprised if people haven't used one form or another already.

      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.

    2. Re:good stuff by ShadeARG · · Score: 1

      I'd be surprised if people haven't used on form or another already.
      Using an autogen.sh generated configure script to prepare a source tree for compilation qualifies as this at some level if I'm not mistaken.
    3. Re:good stuff by a_ghostwheel · · Score: 1

      I use it all the time on large projects. Mostly using either Java or Perl to implement generators (it depends on the source that is feeded to generator). Plus on large projects part of code generation is usually embedded into the desing/modeling tools that are used there (Rational Rose being obvious example). XML/XSLT approach is a way to go now though - I would agree with this.

    4. Re:good stuff by zero_offset · · Score: 2, Insightful
      Ugh. XSLT is a nightmare.

      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 regular old XML DOM based code, and we've done some side-by-side tests that adequately prove (to us; hey, they're our tests) that the code isn't any more concise or easy to write. We already knew it was harder to read and debug. And the current crop of parsers seem to run XSLT a LOT slower than the equivalent DOM calls. It strikes me as a solution in search of a problem...

      --

      Slashdot quality declines as the number of hot grits posts decreases. - Provolt's Law, Apr-09-2005

    5. Re:good stuff by DreadSpoon · · Score: 1

      Ya, I'll admit XSLT is a pretty hard language to work with in the readability department. Especially if you look at my XSLT scripts in AweMUD, which were my first large XSLT scripts. ~,^

      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.

    6. Re:good stuff by Jhan · · Score: 1
      Code generation is definitely something programmers of large/complex projects should look into.

      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 from the linked article:

      Supporting a single database table in an EJB framework requires building up to seven different classes and interfaces...

      Why? This simply means that EJB just doesn't mesh with SQL databases. Which is strange because that's what it was designed for.

      Whenever there's a really harsh conceptual boundrary between two systems, be they programming languages or wildly different API frameworks, meta-programming will be usefull, but not desireable.

      In any event, I would never trust a source code generator, unless I wrote it myself, from scratch. Too many opportunities for mistranslations-heaped-on-mistranslations...

      --

      I choose to remain celibate, like my father and his father before him.

    7. Re:good stuff by kisrael · · Score: 4, 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.


      " V", huh? I feel sorry for your romantic partners. My wang can type " VFR"...
      " VFR4" if I'm really excited.

      --
      SO YOU'RE GOING TO DIE: The Comic for Dealing with Death
    8. Re:good stuff by kisrael · · Score: 1

      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.
      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...

      --
      SO YOU'RE GOING TO DIE: The Comic for Dealing with Death
    9. Re:good stuff by smittyoneeach · · Score: 1

      Used it in a web-enabled database setting, as well.

      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 tables, using straight HTML.

      Then I was accused of over-engineering.

      --
      Get thee glass eyes, and, like a scurvy politician, seem to see things thou dost not.--King Lear
    10. Re:good stuff by roundand · · Score: 1

      OK, XSLT has a high learning curve, and it tends to agitate procedural coders. And I would admit that there's quite a lot wrong with it as a general-purpose programming language. But let's not throw the baby out with the bathwater - and the baby here is XPath, the expression language for XSLT, XQuery and XML Schema, also available for most languages, eg Java and C#.

      Doing XML by pure DOM without XPath is about as productive as querying and updating relational databases without using SQL.

    11. Re:good stuff by Anonymous Coward · · Score: 0

      It's all a matter of formatting and proper comments. I'm using XSLT right now and it works great. A couple of things I do to make code readable is to allow the lines to run longer than 80 columns, since many will, and simply adding comments for the more complicated XPath expressions.

      Using a programming language with a DOM would be okay so long as you can use XPath to interface with the DOM... or is that the messy part?

    12. Re:good stuff by timeOday · · Score: 1
      Code generation is definitely something programmers of large/complex projects should look into. There's a lot of different forms of it, and I'd be surprised if people haven't used on form or another already.
      If I understand "code generation" in this context, the IDL compilers for CORBA implementations are a perfect example of code generation. They write "stubs" and "skeletons," the client and server-side adaptors that implement lots of CORBA ugliness.

      Object oriented database I've used also have code generators, to make persistent versions of C++ classes.

    13. Re:good stuff by zero_offset · · Score: 1

      Don't get me wrong -- I *love* XPath... :)

      --

      Slashdot quality declines as the number of hot grits posts decreases. - Provolt's Law, Apr-09-2005

    14. Re:good stuff by s88 · · Score: 1

      Either you sit in crazy position when you type, or you have something seriously wrong with your anatomy.

    15. Re:good stuff by kisrael · · Score: 1

      Nah, just a lean to the Northwest.

      --
      SO YOU'RE GOING TO DIE: The Comic for Dealing with Death
    16. Re:good stuff by Anonymous Coward · · Score: 0

      Also known as "being right-handed".

    17. Re:good stuff by Anonymous Coward · · Score: 0

      Proving I suppose that the brain really is a more erotic organ than the...veeferfore.

    18. Re:good stuff by whereiswaldo · · Score: 1

      I was involved in a similar project at one time, and the problem with it - although it saves a lot of work - is when you have to make changes to your database schema. If you've hand-tweaked the generated code, you're in trouble. You then have to decide whether to scrap the hand-tweaked code and regenerate everything, or hand-tweak all the code to accomodate the database changes.

    19. Re:good stuff by dublin · · Score: 1

      Code generation is definitely something programmers of large/complex projects should look into. There's a lot of different forms of it, and I'd be surprised if people haven't used on form or another already.


      One good example of how this can be applied in familiar terms is the excellent e-smith Linux server distro. E-smith uses a set of non-trivial Perl templates to generate all the server's configuration files, and another set of templates, based on FormMagick, for its server manager interface.

      While it takes a bit to get fully up to speed on the templating system, (the downside to all code generation systems, but something that in this case is only required for programmers/developers, never users), it ensures that the correct thing always happens at the correct time. Once you realize that permanent changes must be made to the templates, and never the regular config files, as most admins are used to, it's easy.

      The simplicity and robustness that this templating/code generation setup brings to the resulting system is why e-smith is the most solid and easy-to-use Linux *server* distro I've run across.

      (Want a perfectly functioning firewall, mail server and Samba server (with Mac support) in minutes, without even having to know there's such a thing as *.conf files? E-smith is for you...)

      There are other great examples of code generation in practice, but this is one that more people should be aware of...

      --
      "The future's good and the present is nothing to sneeze at." - Roblimo's last ./ post
  3. Software Engineering is not just there yet by stonebeat.org · · Score: 1, Interesting

    Software Engineering is not just there yet. It is a nice concept, but not pratical. Rational Rose does it to a certain extent.

    1. Re:Software Engineering is not just there yet by Anonymous Coward · · Score: 0

      Agreed. Generated code still has to be reviewed and debugged. Plus it is generally bloated. I have seen code generators generate 100K + lines for simple tasks.

    2. Re:Software Engineering is not just there yet by a_ghostwheel · · Score: 2, Insightful

      Nobody is talking about completely automating whole development. But there are two things where code generation helps immensely: 1) "mundane code", like accessor/mutator methods , class/interface definitions, standartized header comments, object mapping - this is covered by generators built into the Rose and similar products. Very helpful. 2) Ability to specify business rules in more efficient form (be it XML, some proprietary language, etc) and generate code appropriate for your framework. This is technique which I personally used several times on large custom software development projects - code quality goes way up. To the certain extent same approach is used in any modern RAD tool.

    3. Re:Software Engineering is not just there yet by johnnyb · · Score: 4, Informative

      Actually, it's very much there. It's not a _replacement_ for development, but there are many parts of coding which is benefitted by code generation. I often write tools to write code for me. Once I had to write a color-picker in HTML (VERY repetitive code), so I wrote a code-generator in Emacs Lisp and it saved me several hours. Several implementations of this concept exist:

      * 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.

    4. Re:Software Engineering is not just there yet by amightywind · · Score: 3, Insightful

      Amen to LISP/Scheme macros. Java and C++ have reimplemented so many other Lisp ideas you wonder why attention never turned to the preprocessor. After 15+ years of C++/Java and OO we would still all be better off programming in Lisp.

      --
      an ill wind that blows no good
    5. Re:Software Engineering is not just there yet by Anonymous+Brave+Guy · · Score: 1
      Java and C++ have reimplemented so many other Lisp ideas you wonder why attention never turned to the preprocessor.

      (say (combine (exclaim Gosh!) (active-clause (subject I) (verb (auxilliary (not can) think)))))

      cout << "I can.\n";

      System.out.println(":-)");

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    6. Re:Software Engineering is not just there yet by Anonymous+Brave+Guy · · Score: 1
      Compile-time programming (only available in LISP as far as I'm aware through the eval-when construct)

      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.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    7. Re:Software Engineering is not just there yet by johnnyb · · Score: 2, Interesting

      Not to the same degree it is possible in Lisp. For example, in LISP, I can actually, say, read a file into a variable for compilation. With C++ I would have to read it at run-time, even if it's completely static.

      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 is that it basically turns into LISP-like code because you have to define everything recursively!

    8. Re:Software Engineering is not just there yet by Anonymous+Brave+Guy · · Score: 1

      Sure, I'm not claiming C++ has the same raw power that LISP does in this respect, just pointing out that it does provide the features to do it.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    9. Re:Software Engineering is not just there yet by Anonymous Coward · · Score: 0

      You're forgetting one type of code generation.

      I type stuff in the "C" programming language and a tool called a "compiler" generates machine code.

      All this "code generation" talk is just a new layer of high-levelness for programming languages. So, of course it doesn't replace development. It is development, with better languages.

  4. Something missing? Like a definition? by Trigun · · Score: 5, Insightful

    Code generation is a time-saving technique that helps engineers do better, more creative, and useful work by reducing redundant hand-coding. In this world of increasingly code-intensive frameworks, the value of replacing laborious hand-coding with code generation is acute and, thus, its popularity is increasing.

    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.

    1. Re:Something missing? Like a definition? by robbyjo · · Score: 1

      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.

      --

      --
      Error 500: Internal sig error
    2. Re:Something missing? Like a definition? by Trigun · · Score: 1

      Now, these people are overloading the same term?? These make me even more confused.

      Which is why I generally don't bother reading more than the back cover of these types of books, and stick to the O'Reilley series, or an equivalent. It seems more and more that the book reviews are just so some slashbot can make a few quid on the referral incentives.

      O'Reilley has never steered me wrong. Slashdot and Amazon however...

    3. Re:Something missing? Like a definition? by arekusu · · Score: 1

      In this world of increasingly code-intensive frameworks ...I guess NSController doesn't count for anything?

    4. Re:Something missing? Like a definition? by register_ax · · Score: 5, Insightful
      I concur.

      [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 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 (Feb 2000) by Brian Greene, or "been around the block more then once" Men of Mathematics (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.

    5. Re:Something missing? Like a definition? by hondo77 · · Score: 1

      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!

      Humans are always going to write subjective reviews. That's just the way it works. If you want reviews that are objective and totally tailored for you, I suggest writing your own book review generator. You might even be able to pick up some tips from that code generation book.

      --
      I live ze unknown. I love ze unknown. I am ze unknown.
    6. Re:Something missing? Like a definition? by register_ax · · Score: 1
      Humans are always going to write subjective reviews. That's just the way it works.
      Which is why I am a transhumanist :p
      I suggest writing your own book review generator.

      I use amazon's featured The Page You Made for possible books. Or (inefficiently) pickup random books at the library and thumb through them (not good for recent books).

      You might even be able to pick up some tips from that code generation book.

      tongue-in-cheek...very funny :)

      Doesn't sound like my rant is good for anything, eh? Actually, I believe /. to be an untapped resource for this kind of stuff...if only there was a better way...a better system...a better...I'll stop before you regurgitate your previous meal ;)

    7. Re:Something missing? Like a definition? by hondo77 · · Score: 1

      Transhumanist, eh? From the site:

      Transhumanism is an interdisciplinary approach to understanding and evaluating the possibilities for overcoming biological limitations through technological progress.

      Are these the guys that are going to make it possible for me to have virtual sex with Christy Canyon? Ah, the future's so bright I'm going to go blind ;-).

      --
      I live ze unknown. I love ze unknown. I am ze unknown.
  5. Re:Was it really necessary? by jared_hanson · · Score: 1

    to ask this question?

    It's a book review for crying out loud. People like to know the specifics of what is covered, and the table of contents helps out. I find it encouraging that the chapters stick to the topic in the title. I'd probably not buy the book if there was a chapter entitled: 10. The F6 key, right between F5 and F7.

    --
    -- Fighting mediocrity one bad post at a time.
  6. Get it from just $25.29! by anonymous+coword · · Score: 1
    1. Re:Get it from just $25.29! by Anonymous Coward · · Score: 0

      again, change yr URL to point to a directory named 'natalie' instead of 'goat' and more ppl will fall for clicking on yr sig URL. your prompt attention to this matter is greatly appreciated. keep on trollin'.

      P

  7. Indian Consultants and Code Generation by Anonymous Coward · · Score: 2, Funny

    This book looks like a poor excuse for management drivel! If we could just get rid of those pesky programmers ... ah! Code Generation! Couple this with the move to push everything offshore ... and voila! no more pesky programmers. Now if we could just get them to finish the powerpoint compiler the development team was working on .... PHB

    1. Re:Indian Consultants and Code Generation by a_ghostwheel · · Score: 1

      Spoken like a true architect. :) Imagine - just you and a bunch of code generators... and nothing to worry about :D

      Damn, it sounds almost like I am ready to welcome code generating overlords.

    2. Re:Indian Consultants and Code Generation by C10H14N2 · · Score: 1

      Last I was forced to code hundreds of entity beans and EJBs, I thanked every god ever worshipped for code generation. There simply is NO reason to hand code this highly structured, standardized, uninspiring dreck. After designing everything in SQL (or Rational, ER studio etc.) why the fuck would anyone want to hand code the same crap again in Java unless absolutely necessary? Given the choice between that and shipping my job off to India, I'd rather learn Hindi.

  8. Wow. H1-B in a can!!! by Anonymous Coward · · Score: 1, Funny

    coding's not hard. just create these dots and we'll connect them for you. just press a button.

  9. Code generation == metaprogramming by Anonymous Coward · · Score: 5, Insightful

    And Lisp has been doing it for years, as has, to a lesser extent, C++ (and Ada) templates. And the good thing about Lisp metaprogramming is that it is in the one language - lisp.

    Having 1 language generating another - Ruby and Java - is the recipe for confusion and complexity.

    1. Re:Code generation == metaprogramming by MrBlint · · Score: 0

      Like a java program that generates javascript code for a web page; that can get really confusing.

      --
      That's very perceptive of you Mr Stapleton and rather unexpected in a G Major
    2. Re:Code generation == metaprogramming by i3spanky · · Score: 1

      Great, Lisp is cool, but generating more Lisp in Lisp is a very limited case.

      I'd wager that most often you really don't want to be writing a code generator in the same language that you're generating.

      Take my last code generator: imagine writing Sybase triggers using Sybase's stored procedure programming language. Trust me, python (or perl/awk/ruby/C) was much better suited to this task.

      Further, as you write more code generators you'll start to develop your own suite of tools, and if written properly these will be highly reusable regardless of the target language.

    3. Re:Code generation == metaprogramming by Anonymous Coward · · Score: 0

      Your wager means that at least one of your languages sucks -- either a bad source, or a bad target, or a bad tool.

    4. Re:Code generation == metaprogramming by a_ghostwheel · · Score: 1

      Having generated code means that you now rely on code generator being properly implemented (usually by few "cream of the crop" developers on your project) vs relying on several dozens of junior developers to code same stuff while they cannot even understand trivial concepts. Usually your experienced developers will end up spending same amount of time consulting rest of the development team that they will spend writing code generators for most of the code (hyperbole but not that far from the truth).

      At least this was true on all large (50+ developers) projects I worked on.

    5. Re:Code generation == metaprogramming by Raffaello · · Score: 0, Troll

      Lisp hackers routinely write code generators for other languages when necessary. They just (wisely) prefer to use lisp as the source language, even if the target is some half baked monstrosity, such as C++, for example.

    6. Re:Code generation == metaprogramming by Anonymous+Brave+Guy · · Score: 4, 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.)

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    7. Re:Code generation == metaprogramming by Joseph+Vigneau · · Score: 1

      Having 1 language generating another - Ruby and Java - is the recipe for confusion and complexity.

      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.

    8. Re:Code generation == metaprogramming by Usquebaugh · · Score: 3, Insightful

      It's a shame that the humor of your post will be lost on the majority of /. readers.

    9. Re:Code generation == metaprogramming by FireBreathingDog · · Score: 1
      It's a shame that the humor of your post will be lost on the majority of /. readers.

      You're right...it was apparently lost on the moderators as well...

      Come on guys, someone with mod points give the grandparent a +1, Funny...pretty please???

    10. Re:Code generation == metaprogramming by Anonymous+Brave+Guy · · Score: 1

      Ah, well, I never wanted to be a comedian anyway.

      [Brainwave] Maybe I should have written

      1. Lex
      2. Yacc
      3. ...
      4. Profit!

      if I wanted the karma? ;-)

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    11. Re:Code generation == metaprogramming by anomalous+cohort · · Score: 1

      What is the difference between code generation in this sense and template based generators such as [aj]sp? You can write a jsp file that generates C++ just as easily as HTML.

    12. Re:Code generation == metaprogramming by Clifford.H · · Score: 2, Interesting

      Metaprogramming it is, but it goes beyond that in allowing you to create your own domain-specific language - and that's really the point. Even modern languages like C# and Java are incredibly long-winded at expressing some types of code, no matter how well you use templating and inheritance.

      What people seem to miss is that computer languages aren't designed for computers, but for people. Lisp is nice (for a machine), but a disaster for ordinary people to read (yes, I've written a lot of Lisp). Perl is even... well, let's just not go there! And whoever imagined that cutesey RPN languages like Postscript, Forth and Smalltalk could ever be mainstream needs their head read!

      Ruby is a fine choice for generators, with all the compile-time power of Lisp, all the advantages of Perl, excellent templating, integrates easily into almost any build environment, and it's readable to boot. It's really the jackpot when considering this kind of task. But to consider Java, C#, C++ or C for all but simplest CG tasks is simply frightening - I know, I've done it multiple times.

      I agree that the title is misleading and said so to Jack during the review cycle (I'm acknowledged for my feedback on p.xix - thanks Jack!). Code generation is about creating, implementing and using a concise human-friendly language for your task. Or compromise a bit and use XML or CSV. You'll loose some readability but gain additional tools, and not have to understand pesky things like production rules and shift-reduce conflicts :-)

      I really look forward to the creation of serious human-usable metalanguages so that generation to today's languages becomes unnecessary. For example, why are EJB's not just a pluggable language extension to Java?

      Anyhow, I like the book, and although I've been writing and using generators for over a decade I'm learning some new things from it - you will too!

    13. Re:Code generation == metaprogramming by GebsBeard · · Score: 1

      Not everyone, that's the funniest thing I've read here in months.

    14. Re:Code generation == metaprogramming by Anonymous Coward · · Score: 0

      Whether one calls them "preprocessors", "code generators", "macro expanders", "template expanders", "front ends", or simply "compilers", the technology to translate higher level languages to lower level languages and is NOT the point. I don't care HOW the higher level language is implemented, the relevant question is "What features do these CG-implemented languges have that make them so much more compact than say EJB or C++?" Also, what (if any) is the performance overhead? Also, how maintainable are programs that are written in these CG-implemented languages?

    15. Re:Code generation == metaprogramming by jjustice · · Score: 1

      >And whoever imagined that cutesey RPN languages like Postscript, Forth and Smalltalk could ever be mainstream needs their head read!

      What does 'RPN' stand for here? And why is Smalltalk 'cutesey'?

    16. Re:Code generation == metaprogramming by Clifford.H · · Score: 2, Informative
      If the CG is well written, the generator and the resultant code is more functional, performs as well or better, and is much more maintainable.

      An example we're using at present takes an XML description of a database schema (our main database is described in 1200 lines without the embedded documentation, 58 tables). It generates perfectly-formatted, commented, readable, and eminently predictable code in five different programming languages, totalling 40,000 lines, as well as high-quality printed documentation. In fact the code is indistinguishable from code produced by a human, except that it lacks human quirks and inconsistencies.

      The generator itself is around 4000 lines of Ruby including all the templates, and has been significantly extended by at least six developers, each of whom found it took them less than thirty minutes to learn enough Ruby to read, understand and extend the generator. The generator itself isn't a simple template expander; it understands and analyses the structure of the database, taking only a few hints to decide exactly which operations are required on each table, relationship and index.

      The generated code includes:

      • SQL DDL (so far only for 1 database product, but additional ones with ease),
      • SQL stored procedures for a range of data access operations including all the bulk operations needed for performance (things like efficient subset-replace from a temporary table, etc),
      • Beans-style C# objects, collections, and nicely segmented access (CRUD) APIs,
      • Composite data type conversion code for the same tables in C++,
      • Business logic base classes
      • Consistent naming, logging and error management across all generated code.
      A further generator in C# augments the generated (and manually subclassed) APIs to produce (sorry, no line count):
      • WSDL interface definitionss for all these APIs,
      • Web Service implementations with location and version brokering,
      • Web service client proxy APIs supporting location transparency (including local linkage),
      I think you'll agree that to generate all this from such a small description file adds significant value over hand-crafting the same. And that's before we start generating user interface, performance predictions and analytical models, test code and adta, etc.

      The generator is integrated into our build environment so that the resultant code never gets checked-in (so is not susceptible to modification) and yet it's always available for debugging. We can subclass all the objects where hand-tweaking and additional methods may be necessary.

      Because it does the grunt work of handling all the bulk operations on the database, those higher-performance features are more likely to get used - developers wouldn't always bother. In these cases, the performance will exceed that of hand-written code.

      Since introducing the generator, we almost instantly found other applications for it (other databases), and also have become much more able to make changes to the schema during the project as needed. Adding a field is a one-line change in one file, not a series of dozens of changes spread across many files, following by a simple recompile. Adding an access method is as simple as adding the index which supports it - the retrieval APIs are implied by the existence of the index.

      A couple of times an additional method has been needed, and it's been added to the generator - this method is immediately available for all relevant tables in the database. Again, adding such methods has been as little as a 2-20 line change in the generator.

      The simple fact is our project is literally months ahead of schedule, and future projects will be even more efficient. As far as I'm concerned, you might as well have asked me what Java or C++ could possibly do that assembly code couldn't have done - the comparison matches at almost every point, with the possible exception of performance.

    17. Re:Code generation == metaprogramming by Clifford.H · · Score: 1
      RPN=Reverse Polish notation.

      Great for pocket calculators when you can remember what the 2 or 3 levels of stack contain, but close to unusable in a programming language, because you can't visually parse it (thinking Forth here) without knowing the arity of every method.

  10. embeded generated code into handwrite code. by Tei · · Score: 0

    Other option:

    You can use external tools (perl scripts,etc..) to generate some really boring files that withouth this tools need to be by hand.
    Code write by hand can have really hard to fix bugs, its better to do it automatically.
    And if everything is sync with "make", you have a beaty machine.

    --

    -Woof woof woof!

  11. Wild by mao+che+minh · · Score: 2, Funny
    "Code Generation in Action"

    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.

    1. Re:Wild by Gr33nNight · · Score: 2, Funny

      Hey bastard, just for your information I put on 20 lbs in the past 4 months!

    2. Re:Wild by Grant_Watson · · Score: 1

      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.

      My room is well lit, you insensitive clod!

  12. We didn't know we were doing anything special... by Fnkmaster · · Score: 4, Insightful
    But we built a very large code generation system as part of my old company's electronic trading system. It used the JavaDoc system and a custom JavaDoc parser we built to generate oodles and oodles of very repetitive code from the base business objects, such as XML parsers and translaters and the like. It was a big time saver, undoubtedly. The big problems we had were versioning and its interaction with our build system, and more importantly, the fact that the code generator itself becomes very complex to read, such that only one or two developers are capable of making changes to it.


    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.

  13. Code generation and Pragmatic programmer by gbvb · · Score: 4, Informative

    if you ever read the "Pragmatic Programmer" book about software developer practices, it mentions that they used code generation for many things. Code generation definitely helps when you have many similar but not same implementation.. But, in Java or any other object oriented languages, inheritence can be used to avoid similar looking code (which is what code generation would do).
    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..

    1. Re:Code generation and Pragmatic programmer by Doctor+Faustus · · Score: 1

      I've used code generation in Java to get database information into objects. My metadata recorded what tables I was bringing in, what the fields were, and what the Java types should be.

      The code generator then created a class for each table, with a constructor that read a JDBC recordset and read each field into the data member, accessor methods, and lookup functions for the fields I needed. It basically took me one day of work on the generator for what would have been four days of annoying, repetitive coding.

      The other neat thing was that I used Access VBA (with Access tables for the metadata) to generate Java code, which is great for the sheer perversity of it. Now I'd probably use XML metadata and XSLT, though.

  14. this post mentions monkeys by turkeyphant · · Score: 0, Offtopic

    Why doesn't it cover the infinite number of monkeys on typewriters method that Microsoft is clearly still debugging?

  15. NOT about compiler code generators by GillBates0 · · Score: 5, Interesting
    I've been doing R&D work with compilers (Ada/C(++)/Java(bytecode generators)) for quite a while now, and the first thought that occurred to me was that this is an article/book about the code generator pass in compilers.

    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.

    --
    An Indian-American Hindu committed to non-violent thought/speech/action alarmed by the global explosion of radical Islam
    1. Re:NOT about compiler code generators by Trigun · · Score: 0

      For the most part KDE/QT is the same thing. Draw a fancy user interface, let it generate the code to show it, then fill in the blanks.

      If they made a program that filled in the blanks, then I'd say "oooh", but this seems pretty old hat now.

    2. Re:NOT about compiler code generators by tomhudson · · Score: 1
      Except that you could apply the same techniques to create c/c++ source files, header files, etc. The whole concept is to create a "tiny compiler language", that does the job you want it to do. Heck, you could even generate assembler if you wanted.

      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.

    3. Re:NOT about compiler code generators by naarok · · Score: 3, Insightful

      A code generator is a compiler. It takes some source and produces an implementation of the source using a more primitive language.

      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.

      Don't dismiss code generation as some trick. It is a very valid approach to simplifying repetitive steps and is as much a compiler as what you are thinking of.

    4. Re:NOT about compiler code generators by kmo · · Score: 4, Insightful

      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.
      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 with a couple of pages of java tied to a Velocity 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.

    5. Re:NOT about compiler code generators by Eponymous+Coward · · Score: 2, Insightful

      Don't get your panties in a bunch! :)
      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."

    6. Re:NOT about compiler code generators by p3d0 · · Score: 2, Insightful

      I do compiler work too, and I think you need to relax a bit. The term "code generator" means "some device that generates code". Just because you misuderstood it at first (as I did) doesn't mean it's wrong.

      --
      Patrick Doyle
      I mod down every jackass who puts his moderation policy in his sig. Oh, wait a sec....
    7. Re:NOT about compiler code generators by Anonymous Coward · · Score: 0

      HTML isn't programming code, its markup. But I'm not sure I'd go so far as to say that it isn't "code." Depends upon how you define code: is code only that which describes applications, or can code also be that which defines how the application should understand the data (i.e., structures data)? This I think is especially when HTML is being used not for its intended purpose, to mark document structure, but rather to construct an interface.

    8. Re:NOT about compiler code generators by Anonymous Coward · · Score: 0

      Code generation is a term of art that has been
      used by the compiler community for decades.
      For example, my PhD thesis is called "An
      Optimizing Code Generator Generator" and it
      came out in 1991.
      Using the term for something else will confound
      indexing and searches. Usually application
      generators are called "application generators".

    9. Re:NOT about compiler code generators by alw53 · · Score: 1

      Usually application generators are called
      "application generators" and code generation
      refers to compiler code generation, and has
      for decades. Terminology is important if
      you want to be able to index and retrieve.

    10. Re:NOT about compiler code generators by sdkone · · Score: 0

      Amen. I took particular offense at the first line from a page describing "Code Generation in Action" which states that "Code generation is the technique of using or writing programs that write source code". I guess I'll have to rewrite all my papers by replacing "code generation" with "stuff that the back-end or JIT does".

      What really irks me is that this Herrington guy seems to be trying to brand his name along side the misappropriated "code generation" title. Especially considering that this ("code generation") is not a new field and has been called other names in the past.

      sdkone gives "Code Generation in Action" a "straight to the trashcan" rating for hubris.

  16. in Action? by messju · · Score: 1

    if i want to see code generation in action i do
    cd /usr/src/linux && strace -F make

  17. ah hell by Anonymous Coward · · Score: 0

    just VS .NET. it does all the work for you. or so the ads say.

  18. Can't be read by Anonymous Coward · · Score: 1, Funny

    Too many buzzwords!

  19. Why ruby to generate java code. by JumpSuit+Boy · · Score: 1

    I wrote a generator that took a sql file and generated a light java database persistance layer. My reasons for doing this, in Ruby and not Java ,boiled down to the horror of manipulating Strings in java vs. the ease of it in Ruby.

    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.

    --
    Oh really?
  20. Am I FUD? by mcc · · Score: 2, Insightful

    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? What if you have a hundred tiny projects, each of which contains the generated code snippet that needs to be changed? Let's hope either the change you want to make is very simple or you are very good at regular expressions.

    If you are able to clearly separate your code into "You can edit here" and "You cannot edit here" chunks, you can DEFINITELY seperate your code clearly into local chunks and delegated chunks-- i.e., "you cannot edit here" means you just do stuff, "you can edit here" means you talk to a delegate object or method. If EJBs are so frigging complicated that you have to do a bunch of repetitive grunt work that's the same every time you do it, you should somehow be building a slightly higher-level abstraction off of the things you do in common on each EJB and working from there. If EJB does not make this possible you should perhaps not be using EJB. There's always some way to do these things through abstraction, and it will ALWAYS in the end wind up more flexible then either generated or cut and paste programming.

    If you've got a code generator sitting around, then sure, go ahead and use it. But I cannot think of any case in an object-oriented language where it would be both less work and more maintainable to write a code generator than to just abstract away the parts that would be autogenerated..

    1. Re:Am I FUD? by revscat · · Score: 1

      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. XDoclet is an example of this: among other things you can use it to generate your servlet container configuration files based totally upon comments in your source code, or your custom tag configuration files, including TLDs.

      Having said that, I completely agree that using a tool to generate actual source code seems to be lazy design. Some form of abstraction/factory/builder/whatever should be able to take care of just about all of these cases.

      Plus, it just strikes me as being so inelegant.

    2. Re:Am I FUD? by querencia · · Score: 3, Insightful

      Code Generation is for people who don't understand or are too lazy for abstraction

      The article that timothy suggested as background reading (here) 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 use it. Managers who have no idea what J2EE is require you to use J2EE. So you use code generation.

    3. Re:Am I FUD? by eggsurplus · · Score: 2, Interesting

      One area where code generation is very handy is in creating java code to interface with a specific database. Based on the type of interface I want (select, insert, update, delete) and the database I can easily create the classes in minutes saving me days of valuable time. In my case I use this to convert data between 2 databases which could be any number of totally different databases. Now only the conversion procedure needs to be coded (for now).

    4. Re:Am I FUD? by neutron2000 · · Score: 1

      Yep ya' are :)

      If you are able to clearly separate your code into "You can edit here" and "You cannot edit here" chunks, you can DEFINITELY seperate your code clearly into local chunks and delegated chunks

      Sure--but I don't want to manually code my local chunks, either.

      I have a completely swell OO system that abstracts functionality and data like it's supposed to but the subclasses are still similar and tedious.

      Dave

    5. Re:Am I FUD? by smagoun · · Score: 4, Insightful
      You're assuming you run the generator once, and that's it. That's what many people do, and it's wrong. The generator should be part of the build cycle. If you want to change the generated code snippet, change the generator!

      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.

    6. Re:Am I FUD? by mcc · · Score: 1

      The generator should be part of the build cycle. If you want to change the generated code snippet, change the generator!

      See, now this I agree with heartily and I think it's a really neat idea. I'm not quite sure I'd do it, but it's certainly a viable strategy. I wish there were a term to separate that from the sort of code generation that seems to be being discussed in the article here and linked. One is generating code and then using it, the other is something akin almost more to defining your own programming language.. I don't know. While the only difference between the two types of code generation is when the generation happens, I really kind of see them as two quite different approaches, one of which is a good idea, one of which is a bad idea.. :)

      Anyhow.. yeah. Something.

    7. Re:Am I FUD? by smagoun · · Score: 2, Interesting

      Take a look at http://sandboss.org for an example of this in action. Sandboss is a tool for building enterprise apps, so it's rather domain-specific. It makes extensive use of generators as part of the build cycle, so you get the benefits without the BS. In the included sample app, a few hundred lines of application source get turned into 150,000+ lines of communication, configuration, persistence, serialization, and UI code (html, servlets, etc). That sounds like a lot (and it is), but that's 150k lines of code that you don't have to worry about maintaining.

    8. Re:Am I FUD? by mprinkey · · Score: 1

      I can think of another area where code generation could be very useful...numerically intensive tasks. There are lots of C optimization techniques (register and cache blocking especially) that are architecture, CPU, and memory subsystem dependent. To optimize for different platforms, you normally need to do explicit loop unrolling and hardwire blocking factors in the code.

      Code generation allows a "higher level" description of the problem in some abstracted language to minimize replication of common code and also allows adaptive selection of blocking factors, etc. to maximize code performance. The Altas BLAS library is an excellent example of the power of the latter which amounts to per-system adapative code generation. The C Preprocessor and C++ Templates are not sufficiently general to allow this same sort of optimization without some code generation segment.

    9. Re:Am I FUD? by i3spanky · · Score: 1

      Good point, but consider that not everything on the planet is object oriented. Much of the code that I generate is SQL (stored procedures / triggers, etc.) -- tough to abstract in the same way that you describe. Sometimes that could be done, but it is likely to only be useful for things that are dynamic at runtime. Sometimes you just don't want to do that kind of work at runtime, and in the world of database applications, (pre-generated, long life) stored procedures offer advantages by allowing the db engine to analyze performance and use that information for further optimization.

    10. Re:Am I FUD? by statusbar · · Score: 1

      This, of course, is exactly how the first C++ compilers were designed. They were really just C code generators.

      --jeff++

      --
      ipv6 is my vpn
    11. Re:Am I FUD? by Anonymous Coward · · Score: 0

      Code generation IS an abstraction. The first C++ compilers were basically C code generators. Anything you can reasonably call a compiler is basically an assembly or byte code generator. Assemblers are machine code generators. Even C compilers don't usually go directly to assembly, but to a kind of RTL as an intermediate target. All the article is talking about is additional steps behind the "high-level language". Something like 'yacc' falls into this category, too.

    12. Re:Am I FUD? by p3d0 · · Score: 1

      Yep, you're FUD if you have ever used a compiler.

      --
      Patrick Doyle
      I mod down every jackass who puts his moderation policy in his sig. Oh, wait a sec....
    13. Re:Am I FUD? by SlipJig · · Score: 1

      Code generation and abstraction are not mutually exclusive. Case in point: our company, which does custom application development, has developed a framework of code that employs a lot of abstraction. However, we do projects for many clients, and we get new clients all the time; as a result every project is a new one, and if we hand-coded it we'd be hand-coding forever. Instead, our code generator produces project-dependent code that leverages and extends the framework code. And our framework code can use the project-dependent code despite the fact that it was generated later. You CAN have it both ways.

      One other thing - I haven't seen this mentioned, but inheritance works well as a mechanism to separate custom code (hand-written) from generated code. We generate placeholder classes for the custom code, and then avoid stepping on them if we regenerate later. There still remains the possibility of breaking your custom code, but the risk is minimized.

      --
      Read my keyboard review.
    14. Re:Am I FUD? by Lodragandraoidh · · Score: 1

      Ummm - how does this differ from just building class libraries for the redundant stuff, and instantiating objects? (or building subroutine libraries, and including and calling the functions from your programs?)

      Seems like 6 dozen of one, and 72 of another...

      --

      Lodragan Draoidh
      The more you explain it, the more I don't understand it. - Mark Twain
    15. Re:Am I FUD? by ketan · · Score: 2, Informative
      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?

      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 build environment to embed a build number and timestamp into the executable? That is code generation. What about a template language? What about something like JSP, which generates Java code from templates? Code generation is everywhere. You use it implicitly. Anything that's not machine code could be considered a code generation template language. Hell, at this point, even that's not true. With both AMD and Intel decomposing x86 instructions to internal RISC-like sub-languages, x86 assembler can be viewed as a template for the decode stages of CPU to generate micro/macro-ops code for the backend. These just happen under your radar. Don't dismiss a universal and useful tool simply because you've seen it done badly. Most of the time it works so well you don't even know it's there.

      Your example with EJB reflects a half-baked design for EJBs. Sun is working to fix that with metadata in Java 1.5, but until then, code generation tools are too useful to ignore. Besides, the only difference is how explicit the code generation step is; it's going to be there regardless.

      --
      You have a choice: tax and spend Democrats, or borrow and spend Republicans. Choose wisely.
    16. Re:Am I FUD? by plierhead · · Score: 1
      Code Generation is for people who don't understand or are too lazy for abstraction

      Not so. Code generation is a subject that a lot of baloney gets talked about. I know a tiny bit about the subject since we kept our company afloat for quite a while selling a complex code generator to OEMs.

      There are two types of programmers IMO.

      1. Those that have built their own little code generator that they think kicks the ass of everyone else's code generator.
      2. Those that think code generators are a load of bollocks.
      Some points about code generators:
      • They are most useful in two situations:
        1. Where you have rigid, fixed and often standards-based formats that prevent you from using abstraction (EJBs being the classic case)
        2. When you have one source medium and many target mediums. For example, you might read UML and use it to generate SQL, HTML, JSPs, EJBs and all the other bits of a multi-tier app.
      • Building the generator part of a code generator is real easy. Building the other bits (what you might call the re-generator) - i.e., that allow users to add their own constructs to the code, edit the generated code appropriately, upgrade the templates over time while still preserving user code, etc. etc. is probably 95% of the work. What most people think of as code generators are simple scripts that spew text into a file.
      --

      [x] auto-moderate all posts by this user as insightful

  21. Wow...what an empty topic! by Junks+Jerzey · · Score: 0, Flamebait

    When I saw the term "code generation," I immediately expected it to be about compilers. That's how the term has been used for, hmmm, 40+ years now. And it kinda is about compilers, sorta, in a bizarre, "charts & diagrams" software engineering kind of way. But you have to hunt to find a solid definition of the modern buzzword "code generation" that doesn't reference other unexplained acronyms and terms. And the review doesn't explain it either!

    This is one of those depressing, faux engineering, topics like "patterns" and "factoring." Once you dig behind all the buzz and feelgood articles, you find out that it's simply a popular renaming of what's been standard practice since before most of us were born. ("Factoring" was a key topic in most Forth books from the early 1980s, but it has been reinvented and rebranded as a cool 21st century thing.)

    1. Re:Wow...what an empty topic! by IpalindromeI · · Score: 1

      I find it surprising and rather humorous that so many apparently intelligent people can be so mislead by a simple two-word term. Sometimes a phrase can simply mean what the words mean when you put them together. In this case, the phrase "code generation" means, lo and behold, "generating code". If you know what "code" is, and if you know what it means to "generate" something, you can figure this out. The problem is that your view of "code" and "compilers" is too narrow. Please see this earlier post on how this is still very much dealing with compilers, although not in the strict sense you're thinking.

      I don't, however, understand how you think this is merely a popular renaming of a long-standing technique, since no renaming has occured. It was code generation 40 years ago and it is code generation today. Back then it meant describing what you wanted to do, and then generating the code that would do it. That is what it means today. The only real difference is that, today, there are more languages in which to do the describing, and more to be generated. There's not that big of a difference, after all, between generating machine code from assembly, and generating Java code from Ruby.

      --

      --
      Promoting critical thinking since 1994.
  22. Yea. Ok. Perl do it, too. by Tei · · Score: 0

    And also Perl provide some features that where very cool to generate Perl. Sooo cool that you can have a Perl app that generate his own source. You can do that with Lisp?

    --

    -Woof woof woof!

    1. Re:Yea. Ok. Perl do it, too. by MrBlint · · Score: 0
      You can do it BASIC very easily
      10 LIST
      --
      That's very perceptive of you Mr Stapleton and rather unexpected in a G Major
    2. Re:Yea. Ok. Perl do it, too. by Anonymous Coward · · Score: 0

      Perl, cpp, m4, C++, and other code-generation systems do TEXT substitution/rewriting. Lisp macros do CODE substitution/rewriting. There's a big difference. "Those that do not understand Lisp are doomed to reinvent it. Poorly."

    3. Re:Yea. Ok. Perl do it, too. by Raffaello · · Score: 4, Informative

      Yes, of course you can do this in lisp.

      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.

    4. Re:Yea. Ok. Perl do it, too. by edhall · · Score: 1
      Perl, cpp, m4, C++, and other code-generation systems do TEXT substitution/rewriting.

      There is a lot more going on here (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 is full of such metaprogramming, such as this.

      -Ed
    5. Re:Yea. Ok. Perl do it, too. by strobert · · Score: 2

      excuse me??? have you ever done code generation in perl?

      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 a bunch of perl code snippets that get require'd in. and soem config files setting variables (using perl syntax) that get eval'd. the system is under 200 lines or perl (including option processing, documentation and the like).

    6. Re:Yea. Ok. Perl do it, too. by Anonymous Coward · · Score: 0

      Whatever your language can do, Lisp has been able to do for twenty years longer. The only downside is that Lisp is obscure, and barely anyone knows it, and no employer will ever hire you because of it.

    7. Re:Yea. Ok. Perl do it, too. by strobert · · Score: 1

      well saying lisp can do everything that perl can do is like saying everything can be boiled down to a turing machine.

      I am sure lisp can do some things better than perl, but I would be hard pressed to beleive lisp can do everything better than perl can do it.

  23. Code Generation in Java... by nettdata · · Score: 1

    I use XDoclets a LOT. I mean A LOT. It does some really cool stuff for us, from dealing with MBean interfaces to just about anything else that you can think of.

    It's also very tightly integrated with ant and maven, the two build tools that are used in just about every project I'm involved in.

    You can find out more about it here.

    --



    $0.02 (CDN)
  24. I guess that is what I did by mathematician · · Score: 1

    I did some code generating in my program xkbset, which is a CLI program to set various parameters of the XKB features of X Window. The program is a bit like xset, and so one might write a line like:

    xkbset bouncekeys 4 -mousekeys sticky -twokey

    to set various parameters. Well basically the program is a loop that goes through argv interpreting the arguments using a bunch of if statements (just like the internal structure of xset). It seemed like way to much work to write all these if statements, so I wrote a "configuration" file that defines the various arguments, and a perl script that turned the configuration file into a C program.

    It was a home grown code generation attempt. I really am quite pleased with it, and it is the only open source program I have written that people actually seem to use (mostly people with Toshiba laptops that need to get rid of key bouncing problems).

    http://www.math.missouri.edu/~stephen/software/# xk bset

    (Somehow there is an extra space added in the URL between the k and the b - it shouldn't be there.)

  25. My Code Generation Experience by mofochickamo · · Score: 1
    I have written several code generators at my place of work. One of them generates SQLServer, Oracle, and PostgreSQL specific code from a generic SQL file. Another generator creates C code for an install program from an install specification (much like InstallShield). Yet another generator create C code for a resource library from a resource specification.

    All of these generators had two things in common: they used Bison as the parser and Flex as the lexer. Since the book uses Ruby in its examples I doubt that it covers Bison and Flex, which is a shame since they are extremely useful tools.

    Another thought I had is this: where is the line drawn between code generation and compiling? Are they the same thing? The Dragon Book (Compiler - Principles, Techniques, and Tools), says "...a compiler is a program that reads a program written in one language - the source language - and translates it into an equivalent program in another language - the target language." They seem like the same thing to me.

    --
    Honk if you're horny.
    1. Re:My Code Generation Experience by stratjakt · · Score: 1

      Code generation doesnt mean compiling. If you wrote a program to translate Java to C or some such, one could call that a compiler. But if you write a program to say, scour a C file and generate the needed #include directives, it's a code generator but has nothing to do with compiling.

      This is a form of code generation I use all the time when I restore a database to test against and screw up all the permissions, which I always seem to do in SQL Server:

      CREATE ROLE DBAdmin
      GO

      SELECT 'GRANT REFERENCES, UPDATE, SELECT, INSERT, DELETE ON ' + name + ' TO DBAdmin' FROM sysobjects WHERE xtype = 'U'
      GO

      I run that and get a nice result set with all the GRANT statements I need. A similar statement gives me execute permissions on stored procedures. Beats typing 75 statements in a row. It's trivial, but its technically code generation.

      --
      I don't need no instructions to know how to rock!!!!
    2. Re:My Code Generation Experience by IpalindromeI · · Score: 1

      Another thought I had is this: where is the line drawn between code generation and compiling?

      My opinion is that they are the same. Whether you're generating machine code from C, or Java code from Ruby, it's really just different uses of the same tool. You write a description of what you want to do, and that is used to generate code that does it. Of course, unless your target code is machine code, it will need to be further compiled, but it's really just another step in the same process.

      --

      --
      Promoting critical thinking since 1994.
  26. Ruby not Java by Colonel+Panic · · Score: 4, Informative

    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..., but with the majority of the code-generation examples being for Java-related technologies, I wonder why Java was not selected instead.

    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.

    1. Re:Ruby not Java by DevilM · · Score: 1

      Find a regex engine for Java? You mean like the one that is included in JDK 1.4?

    2. Re:Ruby not Java by egomaniac · · Score: 1

      Java is not an optimal text parsing language - first off you have to find a regex engine for it.

      Like, for instance, the built-in java.util.regex package?

      --
      ZFS: because love is never having to say fsck
    3. Re:Ruby not Java by mcrbids · · Score: 1

      That leaves you with choosing one of the scripting languages: Ruby, Perl or Python.

      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.

      --
      I have no problem with your religion until you decide it's reason to deprive others of the truth.
    4. Re:Ruby not Java by MrBlint · · Score: 0

      I've written many parsers in java and never once found a need for a regex library. The one time I wrote a parser in perl using regular expresions was a nightmare (What do you mean I can't parse recursive structures). The key to writing a nice clean parser in java is to employ the Interpreter design pattern

      --
      That's very perceptive of you Mr Stapleton and rather unexpected in a G Major
    5. Re:Ruby not Java by aziegler · · Score: 2, Informative

      Earlier this year, I helped review this book during the publication process. At one point, the question was raised whether Ruby was the "ideal" language for this. In my opinion, the answer is "absolutely yes." Ruby -- and Python, if you can get past its syntactic oddities that I can't get past -- is "executable pseudo-code."

      -austin

      --
      Ni bhionn an rath achx mar a mbionn an smacht (There is no Luck without Discipline)
    6. Re:Ruby not Java by aziegler · · Score: 1

      Becausen PHP is an abomination for writing clean code. I'm not saying it can't be done, but it's as common to find clean PHP code as it is to find clean OO Perl code.

      -austin

      --
      Ni bhionn an rath achx mar a mbionn an smacht (There is no Luck without Discipline)
    7. Re:Ruby not Java by tcopeland · · Score: 1

      This hourly build is driven by Ruby and Ant for the exact reasons that you mention - Ruby's great at parsing text (or XML using REXML).

    8. Re:Ruby not Java by spRed · · Score: 2, Insightful

      PHP has nothing to do with text processnig. It is highly specialized as a page-based web language.
      That makes it great for small dynamic sites (which it is frequently and effectively used for) and crap for everything else.

      --
      .sig Karma out the wazoo, better to spend points elsewhere if this is above 2 or below 0
    9. Re:Ruby not Java by s88 · · Score: 1

      "Java is not an optimal text parsing language - first off you have to find a regex engine for it."

      Umm.. ok... JDK 1.4. Whats second off?

      While I'm not particularly arguing for or against the use of Java, I don't think you make a compelling case.

      Also, why not use some of the existing tools?
      * Eclipse's JET
      * XDoclet
      * Velocity
      Not to mention the numerous other specialized generators, GUI builders, and IDE features.

      Scott

  27. Good subject, bad example by msuzio · · Score: 2, Interesting

    The linked article on devx.com really makes me not want to use code generation :-). Let's pick out some fun quotes:

    "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 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 ;-).

  28. not impressed. by Pinball+Wizard · · Score: 2, Insightful
    As engineers we build time-saving applications for others but never think to apply the power of computers to our own problems.

    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?

    --

    No, Thursday's out. How about never - is never good for you?

    1. Re:not impressed. by gregfortune · · Score: 1

      Obviously you understand that it would be just silly of them to push copy-paste practices so what do you suppose they meant?

      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 different.

      Now couple that with the need to make very small specialized customizations to many of the classes/tables and you end up with something that can be generated and the individual generated base classes then subclassed easily, but is very difficult to abstract and generalize without generating the base class.

      Also, consider how you would need to resolve the object-to-relational mapping of a object that had a couple of 1xn relationships, a nxn relationship, and even a self referential nxn relationship. Do you do that at runtime? Everytime the class is accessed? Really? Because that would be necessary if you just create an abstract class that knows the entire data model for the entire application and is responsible for do the mappings for every object.

      Those two issues (forced dynamic lookup and no customization allowed) make the some sort of generic overlord class prohibitive. It's here that code generation makes a whole lot of sense.

    2. Re:not impressed. by William+Tanksley · · Score: 1

      That's why we use functions and classes.

      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

    3. Re:not impressed. by Bytenik · · Score: 1

      Using a domain-specific language to describe data or code is incredibly useful. Sometimes it makes more sense for that language to be interpreted at runtime and sometimes it makes more sense for that language to be interpreted at compile time and to generate code.

      As an example, let's say that you have an text based format that describes dialog box layout and controls.

      A typical design patterns solution might be to create an Interpreter for the file and read it in at runtime and then build the dialog box on the fly.

      A code generation solution would read the file and generate code that calls the appropriate windowing routines to create the required dialog. This code would be compiled along with the rest of the application.

      The code-generated solution will be much faster since there is no parsing overhead at runtime. The design patterns solution will be more flexible since dialogs can be changed without recompiling.

      If you need that flexibility, then fine. If you don't, then why pay the price for it?

      And anyway, since design patterns are just useful solutions to common problems, I would say that code generation certainly qualifies as a design pattern.

      --

      "Scientists prove we were never here."
      -- Devo

  29. Code generators are just compilers... by Dr.+Zowie · · Score: 3, Interesting
    The obvious case is a generator that writes "C" code -- after all, "C" was intended as just a metalanguage like Gnu's RTL, but for the earlier BCMP compiler. It's arguable that generated C code is more true to the spirit of the language than is hand-coded C code.

    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.

    1. Re:Code generators are just compilers... by Dr.+Zowie · · Score: 1

      Jeez, shouldn't post before coffee. That's "BCPL" of course, not "BCMP"...

  30. Been doing this for YEARS by renehollan · · Score: 1
    Heck, I've been doing this for years, generally in the area of test automation.

    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 client/server IDLs is usually sufficient to describe the objects that can be passed (via the building of the stubs for their marshalling/demarshalling), as well as the interfaces themselves.

    Of course, compilers for more traditional languages generally the structural information for object classes directly, and could output this for the benefit of automaticallly generating test clients for a particular interface, but getting that data out of the compiler's symbol table is often like pulling teeth.

    --
    You could've hired me.
  31. Code generation a necessity by russotto · · Score: 3, Insightful

    I work on a large Java project where we use entity EJBs. Code generation isn't an _option_ here, it's a necessity. We have hundreds of tables (over 500) and each of them has an EJB. Writing out the infrastructure for each and every one by hand would be a huge and boring waste of time. I think the necessity for code generation actually points to a problem with design of EJB itself, but that we're pretty much stuck with.

    1. Re:Code generation a necessity by Make · · Score: 1

      Interesting point, a design flaw in EJB itself... I thought of this many times. I never use code generators, OTOH I don't work on such projects, and I would hate to do it.

      Code generation is a kind of compression. i.e., the generated source code has a very high redundancy level. This is what I criticize about code generators: when the generated code is redundant, why not create a better language which expresses my ideas in a non-redundant way?

      The "real" code for your EJB's is stored in GUI forms, not in the code itself... but I want to write my code in a plain text editor. I think I would accept some kind of "super-high level" language which compiles into the redundant Java source code.. the "more geek way" to do it?

    2. Re:Code generation a necessity by bmj · · Score: 1

      You know, this is a good point. I've done my fair share of EJB programming, and now I'm using the Torque 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.

      --
      Whereof we cannot speak, thereof we must be silent. --Ludwig Wittgenstein
    3. Re:Code generation a necessity by BigGerman · · Score: 4, Insightful

      there is a _huge_ design problem with EJB.
      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.

    4. Re:Code generation a necessity by sdkone · · Score: 0

      This is because Java is an single-inheritance, object-oriented programming language, which is great for single inheritance, object-oriented programming. But not for the type of component-oriented programming that EJB land requires. Java just doesn't provide adequate abstraction from the plumbing and as a result, programming EJBs feels like rather an ad hoc process, requiring a whole bunch of junk to be repeated every time the process is started anew.

      In this case, a better solution is a better programming language, with better abstractions for the target domain. "Code generation" is simply a band-aid solution (in this case) that automagically gives you a solution. This is ok if you understand what it's doing but dangerous if you don't. Abstraction, not magic, is the key.

  32. Re:We didn't know we were doing anything special.. by sircrown · · Score: 2, Informative

    You might want to check out XDoclet next time rather than write your own parser/generator. It's pretty widely used now and has lots of tags for many common uses like EJBs, Hibernate, web.xml generation for all the major appservers, etc. It's also integrated with Ant and it looks like Sun is going to be borrowing some ideas for use in Java 1.5+.

  33. If you're going to use big words... by brooks_talley · · Score: 2, Informative

    ...it's really important to use them properly. "No pretense of ambivalence," indeed! What, are reference materials supposed to be ambivalent?

    From http://www.hyperdictionary.com/dictionary/ambivale nt :

    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

  34. Swings and roundabouts by Rogerborg · · Score: 4, Insightful

    I worked at a telco where we generated C code on the fly from high level Structured Definition Language 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.

    --
    If you were blocking sigs, you wouldn't have to read this.
    1. Re:Swings and roundabouts by azlondon · · Score: 1

      We did a similar thing - Used code generation to tweak the data model, and quickly prototype alternate ideas. Once we were happy with it, we cut it loose and worked on the code directly. If we had directly coded it by hand (which would have involved a lot of monkey-work), we would have to stick with the original design, flaws and all.

    2. Re:Swings and roundabouts by DukeyToo · · Score: 1
      In theory, there's no difference between theory and practice, but in practice there is

      Wow, I like that. Thanks for the new sig.

      --
      Most writers regard truth as their most valuable possession, and therefore are most economical in its use - Mark Twain
  35. Congratulations by Trigun · · Score: 1

    Is there a party when you break 100?

    1. Re:Congratulations by Gr33nNight · · Score: 2, Funny

      The sad thing is, I read that 100 as binary ;/

    2. Re:Congratulations by Anonymous Coward · · Score: 0

      I hear that the fire dept comes over when you hit 700# and lifts you to the hospital on a crane!

    3. Re:Congratulations by Trigun · · Score: 1

      Fourteen pounds to go!

      WOoHoO!

    4. Re:Congratulations by Anonymous Coward · · Score: 0

      So you must have had the party a long time ago then, eh?

  36. goatse.info... by Anonymous Coward · · Score: 0

    That is a fucking good site. I think I'll switch from /. to there!!

  37. code generation v. decent languages by po8 · · Score: 1

    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: a classic example, as we were stuck with C as the implementation language (indeed that was the whole point).

  38. um. by pb · · Score: 1

    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.

    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.
    --
    pb Reply or e-mail; don't vaguely moderate.
  39. My website uses code generation by PD · · Score: 1

    My website at http://www.pdrap.org uses code generation. All my pages are static html, and are generated by a Python script that I wrote from a source+template. The source directory is organized the same way that the site is organized.

    When I want to add a new page or section, I just make a new directory and drop a document into it. When I want to add photos to my photo album, I just make a directory and drop my photos into it. The script takes care of everything else. It reads the source directory, generates new pages into a cache area. Then I use the unison program (rsync protocol) to put all the changed pages into the web server directory.

    The end result is that I can add whatever I want to my website without having to write html, or run any CGI, javascript, or anything like that on the webserver.

    1. Re:My website uses code generation by Laplace · · Score: 0, Troll

      Too bad your site looks like ass. And not the good, firm, round female kind.

      --
      The middle mind speaks!
    2. Re:My website uses code generation by PD · · Score: 1

      Thanks, so seldom do I get feedback on it, that your attentions are most needed and most wanted. The beauty of my little system of scripts is that I can completely change how the entire site looks by modifying exactly one template file.

    3. Re:My website uses code generation by aflat362 · · Score: 1
      Python, PHP, ASP, JSP

      Aren't ALL web languages generating code? HTML code that is.

      --

      Conserve Oil, Recycle, Boycott Walmart

  40. Why?! by jon3k · · Score: 1

    Why would you ever create redundant code? With or without code generation? Isn't that why we have modular programming languages? Functions and sub routines? Templates and header files? Am I missing something here? This is the first I've heard of it, maybe I should do a little more reading?

    1. Re:Why?! by DukeyToo · · Score: 1
      Its not so much about redundant code as it is about repetitive code. Consider the following pseudo code:
      class a
      {
      void GetData()
      {
      x = reader(0)
      y = reader(1)
      }
      }

      class b
      {
      void GetData()
      {
      w = reader(0)
      r = reader(1)
      s = reader(2)
      }
      }
      That is pretty typical for data access objects, and extremely repetitive for hundreds of classes.
      --
      Most writers regard truth as their most valuable possession, and therefore are most economical in its use - Mark Twain
    2. Re:Why?! by Petronius · · Score: 1

      for(int i=0;idata.getColCount();i++){
      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?

      --
      there's no place like ~
  41. Re:We didn't know we were doing anything special.. by smagoun · · Score: 1
    There's an open-source implementation of a very similar generation system living at http://sandboss.org. It does everything from bean classes to database schemas, and unlike many code generators it's not tied (implicitly or explicitly) to any particular output like EJBs or XML. Although written in Java, the underlying generator architecture is designed to spit out C code just as happily as it spits out Java. The Sandboss generator infrastructure is part of a larger framework for building message-based enterprise apps, and it makes adding new interfaces to those apps much easier.

    Unlike other generation engines, the Sandboss engine concentrates on the application requirements + not the implementation technologies (EJB/XML/etc). Using Sandboss, you can plug in new generators (to change the persistence layer from EJB to straight JDBC) without modifying your app's code *at all*. Generators like XDoclet embed technology dependencies right into your app's code, which makes switching technologies harder. I'm a developer on the project so perhaps I'm biased, but I think it's pretty damn cool.

    The parent is right to warn about maintenance. Meta-progamming of any kind requires a little more thought + understanding than many coders are used to. Generators are basically power tools, and as with any power tool if you're not paying attention you're likely to get yourself into trouble.

  42. Integrating Java and Ruby by Colonel+Panic · · Score: 1

    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?)

    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, define Java classes, etc... from Ruby. " [and that's the way it should be, use Ruby to drive Java ;-) ]

    You could probably use this to drive ANT from Ruby.

    You can find rjni at:
    http://www.thekode.net/ruby/rjni

    It only recently became available (in July) so it wasn't available in time to be included in the Code Generation book.

    1. Re:Integrating Java and Ruby by tcopeland · · Score: 1

      I think Mauricio is in the process of moving it over to RubyForge...

  43. I thought J2EE was supposed to simplify things by KenSeymour · · Score: 3, Insightful

    A quote from the Sun web site:

    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.

    --
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Albert Einstein
    1. Re:I thought J2EE was supposed to simplify things by random735 · · Score: 1

      thank you! i'm glad i'm not the only crazy person thinking this.... esp. when the j2ee spec changes every 6 months. (just came off a huge EJB project, moved from ejb 1.0 to 1.1 to 2.0, used code generation, etc... )

  44. So what is "Code Generation"? by MegaFur · · Score: 0

    Surprise! It's Yet Another Cross Compiler (but it's not a yacc) :-P

    Oh I don't know. Is this progress or not? I can't tell. One of the articles linked to says that code generation is not a symptom of bad design, but I'm reluctant to start using language two to write the code I originally intended to write in language one to be compiled an executed on computer A.

    But then, on the other hand, we have yacc/bison and (f)?lex. These programs are used for generating C code for certain pieces of (or sometimes all of) a compiler. In a sense, this is the "code generation" that the book is talking about. This means that the idea proposed by the book is not wholey (bad sp) new and this increases the chance that the "code generation" idea is not indicative of a slippery slope into an evil hell of overcomplex code and languages.

    So I ask again: Is this progress? I still can't tell.

    --
    Furry cows moo and decompress.
    1. Re:So what is "Code Generation"? by Anonymous Coward · · Score: 0

      That was my thought exactly but you beat me to it. It doesn't take long before another generation convinces themselves they've reinvented the compiler.

      One fact remains - The problem of generating code can be computationally indeterminate, meaning that you can have code generating code generating code generating code, and it won't necessarily get you any closer to having good code. They already tried templates in C++, but a lot of people seem to hate that. The idea of Code Generation just gives you a crutch for expanding on your language, for which you can already either write a library, or write a new language, and there's nothing new about that. I'd say it has things it's really good for, but I don't see how it's a replacement for anything already out there.

      It sounds more like someone wanted to write a new language, but they were too lazy to compile it into binary, so they just compiled it into another language and use its compiler to get the rest of the way, but hey laziness isn't all bad either.

      But I can't think of how using code generation to get past repetetive tasks is any better than just writing a good library - except when you have to deal with someone else's hair-brained interface - but then, shouldn't a good programmer know how to use shell scripts to generate code that's too repetetive to waste time keyboarding in anyway?
      If it's just based on 'templates'....

  45. www.nataliegoat.com by Anonymous Coward · · Score: 0
    I'd buy that for $1!

    How big a pool of contributors do you think we need to pay NP to give set us up the goat?

  46. Re:We didn't know we were doing anything special.. by Fnkmaster · · Score: 2, Interesting
    Well, we used XDoclet as well. Actually, it was EJBDoclet originally, back when we started using it - this was several years back, mind you. EJBDoclet became a subset of our build system. But our custom JavaDoc modules wouldn't really have benefitted much from anybody's framework, honestly, and we had by then 90% decoupled our system from the EJB way of doing things. For example, we auto-generated EJB session beans that used direct DB access for bulk updates and retrieval, because entity beans were insanely slow. In fact, we were also autogenerating the input files for EJBDoclet at the time, since they were sort of EJB "shell classes", and EJBDoclet generated the rest of the annoying EJB stuff.


    Like I said, I have no idea what people are doing these days, this was back during EJB 1.1 days, pre-2.0, and we had a primarily message-based system that used JMS or other non-JMS compliant messaging systems like TIB. Thank god I don't have to touch EJB/Enterprise Java with a 10 foot pole anymore, since it's one of the most poorly designed systems imagineable - I love a lot about the Java language, but Enterprise Java needs to die a slow death.

  47. Actually, a very interesting topic by SWPadnos · · Score: 4, Informative

    For those who are saying that the term "Code Generator" isn't applicable - it is. Consider a C++ compiler. It may generate asm code, which then gets converted into machine code.

    (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), 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

    --
    - The Sigless Wonder
  48. true story by geekoid · · Score: 2, Interesting

    I wrote a 'software generator'. It worked so well, they laid of 3 coders, and yes I was one of them.

    "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!"

    --
    The Kruger Dunning explains most post on /. http://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect
    1. Re:true story by kisrael · · Score: 1

      I wrote a 'software generator'. It worked so well, they laid of 3 coders, and yes I was one of them.

      Gawd, that sucks.

      Can you share any details?

      --
      SO YOU'RE GOING TO DIE: The Comic for Dealing with Death
    2. Re:true story by Anonymous Coward · · Score: 0

      Reminds me the idiot in vonnegut's player piano who invented a machine to do his job and then got laid off because his invention could do his work. And then the only jobs he was qualified for were ones that were done by machines- and the worst part was that he was not qualified to be a designer/inventor!

  49. What about expressing concepts at a higher level? by sisukapalli1 · · Score: 1

    Could be a little OT...

    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", "d");

    That's it. Now, MyClass has accessor methods, a constructor, and so on...

    my $m = MyClass->new();
    $m->a("Hello");
    my $x = $m->a();

    and so on.

    The difference between this and the Code Generation utilities is that there is no intermediate code for the programmar to tweak. Everything is right there.

    S

  50. Code Generation incorporated in .NET by spideyct · · Score: 3, Interesting

    The .NET Framework has great built-in support for code generation, called the CodeDOM model. It is the mechanism used by all of the Visual Studio IDE wizards.

    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.

  51. OO/Inheritance doesn't eliminate the need by jhkoh · · Score: 1

    I worked on a project where we had a family of classes that obviously shared a common parent class. The child classes were similar to each other -- they each had a large-but-unique subset of a common pool of attributes. For example, Class1 had attributes A, B, and C, while Class2 had attribute B, C, and D.

    There was no obvious class hierarchy to put the classes into, yet there were common methods (getA() and setA()). So I wrote a Perl script that generates the code for the classes, based on the attributes that each class should have.

    Everyone I talked to about this problem said "inheritance should solve this problem, you silly fool!", but as I explained the problem in more detail, each person agreed that a code generator was called for. So, object-oriented languages and inheritance alone don't allow you to avoid similar-looking code, at least in my experience.

    1. Re:OO/Inheritance doesn't eliminate the need by Anonymous+Brave+Guy · · Score: 1

      You mean OO isn't a silver bullet, and generics might be useful after all?! Sacrilege!!!

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    2. Re:OO/Inheritance doesn't eliminate the need by nicodaemos · · Score: 1

      Use OO inheritance to represent common behavior between classes. Common attributes are irrelevant in true OO -- after all, with data hiding you never have access to those attributes except through methods anyway. So you see, it all goes back to behavior.

    3. Re:OO/Inheritance doesn't eliminate the need by Anonymous+Brave+Guy · · Score: 1
      Common attributes are irrelevant in true OO -- after all, with data hiding you never have access to those attributes except through methods anyway.

      Indeed. And those who insist on "true" OO even when the accessor/mutator methods confer little or no practical benefit are exactly the reason we have frameworks as bloated as EJB, and the consequent requirements for a layer of code generators to insulate the programmer's sanity from having to work with them directly.

      Perish the thought that we should compromise architectural purity for concrete, practical benefits, and thus obviate those code generators entirely...

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  52. Is code generation always necessary? by jkauzlar · · Score: 1
    Object relational bridge packages commonly require code generation or xml files in order to be used. I have been working on one that doesn't use code generation or xml files.

    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 generator.

    What is the concensus on this? Especially in corporate development environments?

    1. Re:Is code generation always necessary? by jazir1979 · · Score: 1

      "My question is, wouldn't developers rather use purely object-based packages, if they could, rather than code generators?"

      A resounding YES! I am thoroughly sick of using dodgy code generators / wizards in a certain buggy-as-hell O-R mapping tool .. if you don't do things in a certain order, the xml files will corrupt and you're up shite creek.

      I generally turn to doing just the initial generation using the IDE, then editing the XML by hand thereafter.

      --
      What's your GCNSEQNO?
  53. Great Sentence - save for term paper by mtm_king · · Score: 1

    From a link in the post....

    For those not in on the new lingo, a design smell is a heuristic experience-based signal to an engineer that bad decisions about the logical or physical design of the system have been made upstream from the implementation..

    and

    Why do people use computers? We use computers because they make us more productive. Using a word processor is more efficient than writing a manuscript in pencil. An electronic spreadsheet is more efficient, accurate, and valuable than its paper counterpart

    The guy is a genuis...

    Delphi generates just the right amount of code.

    --
    world was created 5 seconds before this post as it is.
  54. Re:We didn't know we were doing anything special.. by smagoun · · Score: 1
    XDoclet is awesome, but it makes a fundamental mistake IMHO. XDoclet requires that you embed specific technology dependencies into your app's source code. The javadoc tags are specific to EJB or JBoss or whatever, so if you ever want to switch to a different tech for something you have to clean out the old tags and insert the new tags into every file. That's exactly what things like EJB have been trying to avoid, and as with EJB, XDoclet doesn't quite get the job done.

    Instead, XDoclet should let you specify what you want accomplished, not how you want it accomplished. The code generation engine should be smart enough to figure out how to accomplish the specific tasks. For example, you should be able to tell the generator "this object gets persisted," not "this object should have a corresponding entity bean named Foo that can be found in JNDI as FooEJB." That way you can switch to straight JDBC by swapping out your generator, instead of changing all those javadoc tags.

    Granted, this approach isn't appropriate for all projects (especially legacy apps). It makes writing new apps much easier though, and if you ever have to switch app servers or web frameworks you'll be thrilled.

  55. carefully now... by *weasel · · Score: 1

    ... people round these parts don't take kindly to any positive talk bout things from redmond.

    yer liable ta start a fight with that kinda talkin.

    --
    // "Can't clowns and pirates just -try- to get along?"
  56. Comment removed by account_deleted · · Score: 2, Informative

    Comment removed based on user account deletion

  57. Oh ye lex and yacc... by evilpenguin · · Score: 2, Informative

    Metaprogramming can be a useful and time-saving technique. It can also really mess up maintenance and future refactoring of a project. The time saved one developer isn't a good measure of the utility of a technique.

    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.

  58. Code generation and macros by game · · Score: 3, Interesting

    Generating code from database schemas and the like can be acceptable in my book. However, generating code just because it is repetitive and clunky is *not*.

    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.

  59. Been there for some time now :-) by HarveyBirdman · · Score: 1

    I started writing Visual Basic programs that generate VHDL code about 8 years ago. If I could get my act together and tighten up (read: productize the GUI) the program that generates Viterbi decoders, I could probably sell the thing.

    --
    --- Ban humanity.
  60. Not FUD, but not correct, either. by tmoertel · · Score: 4, Insightful
    mcc wrote:
    Code Generation is for people who don't understand or are too lazy for abstraction ...
    Baloney.

    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:

    • Single, authoritative reference specification. The object interface was always in sync with the reference, which for this project was the database schema.
    • Richer compile-time error detection. The projection of the schema into the object interface was fully available to the type system so that many kinds of client errors could be caught at compile time, not run time.
    • Reduced opportunity for errors between subsystem boundaries. Because the object-based interface was generated by machine from the actual database -- and not derived from some programmer's understanding of the database -- there were fewer opportunities for impedance mismatch across the boundaries of the application code and the database. (Studies of errors in complex projects have shown that errors are more common between subsystem boundaries, and so this benefit is important.)
    mcc further states:
    But I cannot think of any case in an object-oriented language where it would be both less work and more maintainable to write a code generator than to just abstract away the parts that would be auto-generated.
    If you can't think of any such cases, it's because you're thinking too small. Look at the bigger picture. For starters:
    • When the number of variables affecting the desired code characteristics is large enough to make hand-coding (at any level of abstraction) impractical. E.g., FFTW: "FFTW uses a code generator to produce highly-optimized routines for computing small transforms."
    • When your code must conform to an external reference specification that changes rapidly enough to make hand coding (at any level of abstraction) impractical. (See my example above.)
    • When the requirement for correctness is so stringent as to make hand-coding methods impractical, mandating code generation from a formal specification.
    • When you must target an output language whose native abstraction capabilities are too crude to capture directly the degree of abstraction that is merited. Believe it or not, most popular OO languages fall into this category for many commonly occuring problems. Hence the popularity of design patterns. (Compare, e.g., with the abstraction capabilities of modern functional programming languages like Haskell and O'Caml.)
    Make no mistake about it, code generation is a practical, effective tool that every programmer should understand. To dismiss it out of hand is a costly mistake.
    1. Re:Not FUD, but not correct, either. by Anonymous Coward · · Score: 0

      "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."

      you wrote a vendor language to sql compiler you fucking retard

    2. Re:Not FUD, but not correct, either. by mikewolf · · Score: 1

      right on. DB-based objects are the best example i can think of where using code generation is very powerful, and is absolutely not lazy...

    3. Re:Not FUD, but not correct, either. by crucini · · Score: 1

      This is a great post, but all the cases for code generation boil down to the last - target language does not have the right kind of abstractions. I have used code generation to generate C and other things. But I can't imagine auto-generating Perl, because Perl is more flexible. Actually, I once did grind a big spec through a Perl script to generate Perl that I then maintained by hand - but what I generated was a big data structure describing the spec, rather than a bunch of actual procedural code. The core engine that interpreted the data structure didn't need to change. I could have accomplished the same thing by having the live code parse the spec every time it ran, but there was some data I needed to clean up after the import, and I didn't want the speed penalty.

      I agree about popular OO languages - they're a poor fit for a lot of business and web applications.

    4. Re:Not FUD, but not correct, either. by Anonymous Coward · · Score: 0

      you wrote a vendor language to sql compiler you fucking retard

      No he didn't you retarded fucking retard.

      He wrote a code generator that created "an object-based interface". Is that too hard for you to grasp?

      He said "object-based", which almost certainly means he generated C++ or Java (perhaps C# et al). Get it now?

      Wipe the shit out of your eyes and try reading and understanding before you hit the little Reply button.

  61. Shameless Paul Graham link by Anonymous Coward · · Score: 1, Interesting

    Someone mentioned Lisp macros? Here's the obligatory Paul Graham links, posted AC to avoid karma-whoring:

  62. Lex and Yacc? by teece · · Score: 1

    Isn't this the same idea as using lex and yacc? Granted, most folks think of using those for compilers, but they can be used to do anything.

    In fact, one of the chapters in Lex and Yacc from O'Reilly is a "menu generation language" to automate the tedious task of writing curses interfaces.

    Am I missing something? Lex and Yacc have been around for decades.

    --
    -- Hello_World.c: 17 Errors, 31 Warnings
  63. Code Generator, n. by Citizen+of+Earth · · Score: 1

    Code Generator, n.: A program for generating code that is buggier than itself.

  64. wow, how _else_ do you guys do some of this stuff? by Anonymous Coward · · Score: 0
    Amazing, a whole book on something I thought was obvious. Didn't you programmer types ever write a program that printed out a big static array in C? Look at an .XPM graphic someday.. it's a little bit of C code.

    How do you folks convert database objects to code, you mean you actually write this stuff out by hand over and over again:

    getThing() { return thing }

    setThing(arg) { thing = arg }

    I have better things to do!!

    One thing I don't really like about code generation is that the code generator/templates are outside the language as an "extra step" Dynamic languages like Perl and Ruby *really* make this a useful feature because you can bake it right into the language using runtime creation of methods and/or introspection.

    For instance *all* my Perl DB code looks like this:
    Package Employee;
    use base 'Persistence';
    import Persistence;
    persist_id_field('employee_id');
    pe rsist_data_fields(qw( first_name last_name ));
    persist_table('Employees');
    # end of file.
    and then usage:
    $foo = new Employee;
    $foo->first_name('Fred');
    $foo->last_n ame('Flintstone');
    $foo->commit;
    print "inserted/updated employee #" . $foo->id . "\n";
    In Ruby it's even more elegant since the language is designed for this sort of thing:
    class Employee
    include Persistence
    db_table 'Employee'
    db_id :employee_id
    db_fields :first_name, :last_name
    end

    foo = Employee.new
    foo.first_name = 'Fred'
    foo.last_name = 'Flintstone'
    foo.commit
    Really, if you're EVER programming and you find yourself repeating the same steps over and over, don't sit there, AUTOMATE them!
  65. Patently Untrue by Mark_MF-WN · · Score: 1

    JBuilder and Eclipse, arguably two of the most popular Java IDEs, do tons of code generation for you.

    Seriously, what kind of programmer is wasting their time writing accessors and mutators anyway?

    This goes doubly so for generating skeletons of implemented interfaces or extended subclasses. Java coding benefits so strongly from this that it's almost unimaginable to do it any other way.

  66. not java - ruby by vudmaska · · Score: 1

    I'd guess the author chose ruby not java for pragmatic reasons inherent to interpreted languages over the rigidness of a compiled ones. You dont need the speed of a compiled language in the context of the article. Besides 'harnessing tools' was not the subject - but i'd bet ruby has similar fascilities that were not mentioned. Belittling the author's choice of tools assumes that you understand the problem more so than he.

    --

    my other sig sucks less

  67. Oh yes it is, it's just not widely accepted yet by msobkow · · Score: 1

    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 servers, different network interface technologies, different system architectures, etc.)

    In a sense, I've spent years trying to "teach" my computer to produce code the same way I do by hand. With any luck, it'll be production ready in another few months, and used to service a few early adopters as a test replacement for the offshore junior coders that write the same code by hand.

    The question isn't whether the technology is feasible, but whether I can make a living by providing code factory services.

    --
    I do not fail; I succeed at finding out what does not work.
  68. First generation by msobkow · · Score: 1

    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.

    --
    I do not fail; I succeed at finding out what does not work.
  69. Danger of Code Gen: Dumbing down of programming by ChaosDiscord · · Score: 0

    Code generation has its place, but one needs to be ever watchful of the dangers presented by anything that claims to make programming easier. In summary, it risks creating a bunch of code you don't really understand and can't debug. Ellen Ullman's 1998 article "The dumbing-down of programming" is still good reading.

    Everything in moderation.

  70. Yes: it's about covering weaknesses by Anonymous+Brave+Guy · · Score: 2, Insightful
    Code generation can allow a developer to compensate for missing abstractions in the underlying language or architecture.

    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, but if you find yourself writing generator code too often, you're probably using an underpowered and/or overcluttered language to start with.

    The type-safe enum idiom in Java, which several people on this thread have mentioned, is a great example. In a language with native support for enumerated constants, such as C, the generator and idiom would be unnecessary; you'd simply write the code. In turn, in a language with stronger support for disjunctive types and pattern matching, a lot of the hackery you see with enums and switch in C is also unnecessary. But some people have to use Java and would find enums useful, and some people have to use C and would find pattern matching useful, and for these people, code generation can be a way to simulate the real thing acceptably well.

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    1. Re:Yes: it's about covering weaknesses by ProfKyne · · Score: 1

      The type-safe enum idiom in Java, which several people on this thread have mentioned, is a great example. In a language with native support for enumerated constants, such as C, the generator and idiom would be unnecessary; you'd simply write the code.

      Perhaps it is my misunderstanding, but I thought that the C version of enums are nothing more than integers -- thus, there is no type safety whatsoever to speak of. And this can be easily done in Java (or just about any other language). Whereas the type-safe enum pattern is about making sure that only a reference to the enumerations is allowed to be used, so it's impossible to slip in an illegitimate value.

      Like I said, I could be wrong since I'm not much of a C programmer, but I thought that the C compiler simply looks for an integer, which means theoretically a person could pass it any integer and not just one of the enumerations.

      --
      "First you gotta do the truffle shuffle."
    2. Re:Yes: it's about covering weaknesses by Anonymous+Brave+Guy · · Score: 1

      You're half-right. An enumerator can be implicitly converted to an integral value, but not the other way around. You have to cast explicitly if you want to turn any other type of value into an enumerator.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
    3. Re:Yes: it's about covering weaknesses by ProfKyne · · Score: 1

      So, just so I understand this, you can use an enumeration as an integer, but you can't pass an integer into a function that expects one of the indicated enumerations?

      --
      "First you gotta do the truffle shuffle."
    4. Re:Yes: it's about covering weaknesses by Anonymous+Brave+Guy · · Score: 1

      Basically, yes. You can never convert an integral value (1, 3, whatever) into an enumerator (Red, Green, whatever) without an explicit cast. This includes passing parameters to functions expecting an enumerator, returning enumerators from a function, assigning to a variable of an enumerated type, etc.

      --
      If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  71. ASP/JSP by Anonymous Coward · · Score: 0

    dynamic code generation and compilation is a good thing when it is used for things dynamic page compilation. But it can also lead to really bad habit that result in un-maintainable code.

  72. One of the tennets of the UNIX philosphy... by zmower · · Score: 1

    is "write programs that write programs". Is it because GNU is not UNIX that people don't know this? ;-)

    --

    Sig pending!
  73. Interesting. by Tei · · Score: 0

    1: Thanks to read my post. With bad karma I think is really hard to be read by somehome.

    2: Very interesting your post. I dont know that Lisp feature, black magic. Sad that Lisp have these crippling ((( )))) [[[]]]] here and there. Most langs dont have that because is something complex. I dont think is something good.

    3: My post whas about to quote strings, because mixing langs (code that generate code) is something hard because you end with very very complex escape sequences. The q() and other nice Perl features able to nicely write a Perl app that write Perl code.

    4: I am a old Forth user. I know about langs that build langs. Its funny, but somewhat ofuscated itself. IMHO.

    --

    -Woof woof woof!

  74. Is code generation always necessary? by jkauzlar · · Score: 1

    Object relational bridge packages commonly require code generation or xml files in order to be used. I have been working on one that doesn't use code generation or xml files. 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 generator. What is the concensus on this? Especially in corporate development environments?

  75. Let me tell you why SW engineering is not there ye by Anonymous Coward · · Score: 0

    The main reason is the insane drive towards features, the disinterest in the quality of software, the heavy bureocracy and the unrealistic schedules.

    In other words: nothing else matters except having feature sets x, y and z. Also a, b, c and d. And e. And we want f too. And it's due in two weeks, fully documented, tested (when there's practically no time to test), and bug free of course. And remember, you have to dick around doing other stuff too, effectively cutting the time into 60 % of the original.

    Process means shit if it's just to satisfy some obscure checklist with no real purpose.

    Why can't people understand the simple truth: the easier you make it for programmers, the more high quality and timely your software will be!

    * End of whine *

  76. Descriptive code vs. code generation by booch · · Score: 1
    OK, I'm clearly of the opinion that a lot of "code" should be descriptive instead of procedural. It just doesn't make sense to explain the steps to create a GUI, when you can simply describe how it should look and act, and have the computer figure out how to best implement it. (Of course the actions bound to events would generally have procedural code behind them.)

    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 advantage of being able to convert a descriptive file at run-time seems to outweigh the benefits of doing it at compile time. You can change the GUI without having to compile at all. Which allows non-programmers to more easily make changes. (And since it's descriptive, non-programmers are often better at doing the work.)

    Besides efficiency, you've given the only reasonable explanation that I've seen for code generation versus dynamically doing the work from the description. I.e. compile-time error checking and info in the object code. But I don't find that to be all that helpful either. If the descriptive file has errors, it's not going to work either way. And you're going to have to do your error checking on the descriptive code the same way, whether you're doing it at compile time or run-time. Compile-time checking of the generated code really doesn't give you much either, except to debug the code generator.

    --
    Software sucks. Open Source sucks less.
  77. YOU'RE NOT ENGINEERS! by Gay+Nigger · · Score: 1
    From the article:

    As engineers we build time-saving applications for others...

    Writing code does not make you an engineer. Official certification and certain burdens of legal liability that extend far beyond what most (if any) programmers, and webmonkeys especially, face doth an engineer make. With software written today in such a sorry, buggy state, you can hardly call yourselves engineers anyway.

    1. Re:YOU'RE NOT ENGINEERS! by Anonymous Coward · · Score: 0

      Hey, if the guy pushing a broom can be an engineer, so can we!

  78. Good and Bad by crucini · · Score: 1

    Code generation can have a legitimate place. I guess there are two logical ways to use it. Given a processor that transforms "metasource" into "source", either you maintain the metasource and never hand-edit the source, or you run the generator just once to create a rough framework, and then discard the metasource and maintain the source. I've done both, saving time and aggravation.
    But code generation can be a sign that the wrong language is being used. The author recognizes this, but points out that the individual programmer may not be able to make that choice. True, but the example he gives of generating EJB's seems to show how flawed the Java/EJB idea is in handling database-backed apps.
    Perl inherently supports a more generic approach to data that mates more seamlessly with relational databases. For example:

    my $user = $dbh->selectrow_hashref(
    "select * from user where id=?",
    undef,
    $userid,
    );
    # now the user record is a hashref we can manipulate:
    $user->{ favorite_color } = 'blue';
    # store it
    update_row('user', $user);

    (I'm not recommending this particular sequence of events, of course - slight concurrency problem.)
    update_row() is not standard, but most Perl programmers can see how it's written. It's generic, so it can update any table as long as the primary key is called 'ID'. And it leverages Oracle's caching so the SQL is not recompiled.

  79. Generate code from object descriptions... by Cpt_Corelli · · Score: 1
    I have had a look at some .Net code generators and have seen a lot of them popping up lately. One of the more interesting ones is Visual Welder from Sembleware which will create a complete web application (although with a horrible GUI) from the object descriptions and relationships you set up in a 3D view(!).

    Their webpage says you can create simple business rules as well, but I have yet to try this feature.

  80. (OT) Re:I guess that is what I did by IpalindromeI · · Score: 1

    (Somehow there is an extra space added in the URL between the k and the b - it shouldn't be there.)

    The space is due to Slashdot's lameness filter. It inserts spaces into very long unbroken lines of text so that they will wrap in your browser window and not make the window too wide. For the links, the easy fix is just to create a clickable one like this:

    http://www.math.missouri.edu/~stephen/software/#xk bset

    Then it doesn't matter if a space is inserted because you can just click it to follow.

    --

    --
    Promoting critical thinking since 1994.
  81. If you don't like code generators by Merk · · Score: 1

    Then out of protest, don't use "make xconfig" if you rebuild a kernel. That is done using "tkparse":

    'scripts/tkparse' is a C program with an ad hoc parser which translates a Config Language script to a huge TCL/TK program. 'make xconfig' then hands this TCL/TK program to 'wish', which executes it.
  82. generated code for performance by bob_jenkins · · Score: 1

    The use I've seen for code generators is performance. They've all been in C, writing C code. One avoids a swarm of if-statements by generating 64 different routines for 64 different cases, then the caller calls one of them based on the current input. Another writes a perfect hash for a tokenizer based on the current set of tokens. Without generated code, we'd have to use slower code instead.

  83. Are you FUD? You are not PhD. by jefu · · Score: 1
    But I cannot think of any case in an object-oriented language where it would be both less work and more maintainable to write a code generator than to just abstract away the parts that would be autogenerated.

    To be polite: "Bullshit" (The impolite version, well, best it remains unexpressed.)

    I have a program that has a bunch of base classes and that I specialize with a set of fairly simple java classes to generate domain specific programs. These java classes look very much alike, but vary in as many as 20 lines per class - this includes the name of the class (both the class name and a string representing it) and some executable code. I also generate a list of the classes as a step in building an interpreter.

    So, I wrote a short python program that reads a file describing all the classes to generate and that produces a java class for each such file, a list of the classes, and an executable code segment that loads all the classes and properly initializes them.

    This step makes it possible to generate all these classes quickly when I move the program to work in a new domain.

    There are other ways to do this, but this seems to me by far the most simple and it has saved me much work over time.

    In any case, what is a programming language in general but a code generator for a specific domain (or large connected set of domains)?

  84. Here's why compile-time code gen is often better by tmoertel · · Score: 1
    booch wrote:
    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 advantage of being able to convert a descriptive file at run-time seems to outweigh the benefits of doing it at compile time.
    Code generation and what you are calling "descriptive code" are both methods to derive an implementation from a specification (a "description file"). The difference between the methods is that the former does the work at compile time, and the later, at run time.

    Deciding which method is better, like most programming decisions, depends on the situation. In the case of user interfaces, it is advantageous to be able to change the UI after the code has been compiled; therefore, run-time derivation is probably a better choice. In the case of my object-based database example, compile time derivation allows the compiler to do the important and otherwise expensive job of ensuring that the back-end database and the overlying application are in sync, and so it was a better choice.

    Later, booch wrote:

    But I don't find [compile-time checking] to be all that helpful either.
    You're missing something important here. The point of using an external specification (i.e., a descriptive file) instead of hand-written code is that you can create extremely high-level abstractions that are ideal for the job at hand, and then use those abstractions to "write" your specification.

    Once you have a suitable specification, then you can begin to weigh the differences between the compile- and run-time methods: By doing the derivation at compile time, you are free to use tools and languages beyond your ultimate target language. For example, even if Java is your target language, you can use Ruby or Haskell to convert your specification into Java. This lets you choose the tools and languages that are best suited for representing powerful abstractions and translating your specifications into code. With run-time derivation, however, you're limited to your target language, even if it's a poor choice for your desired abstractions.

    If the descriptive file has errors, it's not going to work either way. And you're going to have to do your error checking on the descriptive code the same way, whether you're doing it at compile time or run-time.
    The problem with this thinking is that, if you use run-time derivation, you're more likely to use weaker abstractions in your specifications because you're tempted to constrain your abstractions to those that are easiest to represent in your target language. When you use weaker abstractions, you place a higher burden on the people who must write the specifications, and that burden results in a greater chance for error.
  85. Best code generation tool by heironymouscoward · · Score: 2, Informative

    Is GSL (aka GSLgen), part of the RealiBase OSS toolset from iMatix.

    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.

    --
    Ceci n'est pas une signature
  86. Of course it's an "enthusiastic overview"! by Anonymous Coward · · Score: 0

    If "code generation" isn't a familiar term to you, this enthusiastic overview on devx.com is a concise introduction to what code generation is about, though it makes no pretense of ambivalence about its importance as a programming tool.

    Of course it's an "enthusiastic overview" on devx.com.

    That article was written by Jack Herrington.

    The author of the book being reviewed is Jack Herrington!

    Hardly an objective overview!

  87. you know youve got it when.. by butane_bob2003 · · Score: 1

    ..your code generator can refactor generated code when it recognizes duplication.

    --


    TallGreen CMS hosting
  88. Re:Here's why compile-time code gen is often bette by booch · · Score: 1

    Ah, you're right -- I did miss the point of having more language tools to do the "conversion" at compile-time. I do realize that the choice of methods is situation-dependent. I just think that like most modern programming situations, programming time is more important than program efficiency. And compile-time code generation tends to take more programmer time. Really this is just the same old compiler vs. interpreter argument applied to descriptive code instead of procedural code. Today, it seems that the best answer is usually somewhere in between (Java, Perl, Python). Unfortunately, I still don't see descriptive "code" being given first-class status by most people.

    I'm not buying your claim about run-time derivation and abstraction levels, since the target language is likely to be the same either way, even if the translation language is different. In other words, if your target language is Java, you're going to have to implement the resulting executable code in Java either way. I suppose there may be a top-down versus bottom-up distinction in the abstraction development process between the two methods though.

    BTW, is there a well-used term to distinguish run-time derivation from code generation?

    --
    Software sucks. Open Source sucks less.
  89. I don't think so... by TheAwfulTruth · · Score: 1

    HTML was envisioned to be exactly what you describe. A description of how the data should be displayed with the details left up to interpretation by the browser.

    That was a huge mistake. It turns out that people want their pages to look EXACTLY like they want, ALWAYS. So every large site today used tables within tables within tables, frames or other add on control mechanisms to make their pages do exactly what HTML was never supposed to do, define exactly, to the last detail, how the data is to be displayed.

    I would really hate to see a GUI system that was at liberty to decide the optimal arrangement or display of controls for my app (And of course different on every system). I find systems that do even the tiniest amount of that (Such as the auto-layout features of the Tk in Tcl/Tk) to be extremely dissapointing. The layout always has problems that cannot be gotten rid of with more description.

    In the end, it's the details that make you come rocketing back to hand coding to exact specifics.

    --
    Contrary to popular belief, coding is not all free blow-jobs and beer. Those things cost MONEY!
    1. Re:I don't think so... by booch · · Score: 1

      No, the problem with HTML isn't that it's descriptive. The problem is that people are trying to make it do things that are completely contrary to what it was designed for. HTML was intentionally designed to leave the rendering implementation-defined. Here's a clue to the idiots that think that was a bad decision: try reading the same page on a cell phone PDA as on a 23-inch display. Either the PDA is going to be unreadable, or the 23-inch display will have 4-inch text.

      To further see that the descriptive nature of HTML is in no way tied to the rendering being implementation-dependent, take a look at XSL-FO. It's basically an XML dialect (thus descriptive and similar to HTML) that does what you think HTML should do: it specifies exactly what a page should look like. Basically it's the same as PDF. Another example would be SVG, although I think you can do some procedural things in SVG. PostScript and PDF are somewhere between procedural and descriptive, much like Lisp.

      As for GUI layout, again the automatic layout and desriptive code are completely separate. (Actually, GTK almost always uses automatic layout, even in procedural code.) Take a look at GLADE. It can either generate C code or you can use libglade to render the GUI at run-time according to the description in the XML file. They both work almost exactly the same, and the layout ends up being the same. Libglade offers some advantages in that you can allow the user (or a GUI designer) to change the layout after the program has been delivered. And it's a lot easier to use libglade in a wider variety of programming languages.

      Besides, details are actually easier to specify in a descriptive file than in program code. You simply add an attribute and a value, instead of having to find the right function call and arguments to use.

      --
      Software sucks. Open Source sucks less.
  90. Most common example : generating SQL by Bazouel · · Score: 1

    Generating SQL on the fly ...
    Who haven't done this at one point or another ?

    You can even make SQL generates SQL !

    I think this is one of the most common example of generating code ...

    --
    Intelligence shared is intelligence squared.
  91. OT: Re:I belong to the Code Generation... by JamesOfTheDesert · · Score: 1
    Not really Richard Hell, since "blank" -- the word you've changed -- was his only contribution to that lyric. The rest goes back to Rod McKuen (or "the woeful Rod McKuen" to give him his full title), who wrote "I Belong To The Beat Generation"

    Well, to be fair, there's a bit more to that. Hell would likely *enoy* people knowing where he cribbed from.

    --

    Java is the blue pill
    Choose the red pill
  92. My favorite code generator: swig by neves · · Score: 1

    Since I believe in Ousterhout's dichotomy, my favorite code generator is SWIG. It automatically create bindings for calling C and C++ libraries from scripting languages like Python, Lua, Perl, Ruby, PHP, and Tcl.

  93. How about now? by tmoertel · · Score: 1
    booch wrote:
    I'm not buying your claim about run-time derivation and abstraction levels, since the target language is likely to be the same either way, even if the translation language is different.
    This argument misses three important points: First, even if the target language is the same, at compile time you have the luxury of targeting the target language's compiler and leveraging all of its power. At run time you have no such luxury.

    Second, the code that is responsible for the run-time derivation must be written in the target language, and if the target language makes the derivation difficult (because, e.g., it is too low level), you will do more work than necessary -- or fall back to weaker abstractions in your specifications.

    Third, in the run-time case, all of the code that executes the derivations (e.g., an interpreter) must be written by hand and hence be readily understandable by humans. Compile-time code generators need not suffer this higher burden: They can generate incomprehensible output code because no human is meant to maintain it.

    To convince yourself of the merits of my counterpoints, consider the case of parsers. The specification is a file defining a grammar. Your job is to create a parser for the specified grammar, and you know that the grammar will change occasionally. Now, you can generate the parsing code at compile time (in effect, writing something similar to yacc or antlr), and this task is straightforward.

    Or, you can try the run time approach. Of course, at run time you won't have the facilities of the target language's compiler at your disposal. Therefore, you'll have to code -- by hand -- a general-purpose parsing apparatus that you can reconfigure at run time based on the input grammar.

    Spend a few moments thinking about how you might approach this coding problem. It's much harder than the equivalent compile-time problem. Why? Because at run-time you're operating at a much lower level of abstraction. You can no longer leverage the abstractions provided by the compiler. You're stuck at the level of run-time abstractions, which is a subset of what you can do with the compiler. For example, it's easy to create new classes at compile time but very difficult at run time.

    Finally, booch asked:

    BTW, is there a well-used term to distinguish run-time derivation from code generation?
    Not that I know of.
    1. Re:How about now? by booch · · Score: 1

      You've convinced me in some ways, but not in others. I definitely buy your first point. But I have a problem with your position that compile-time allows for higher levels of abstraction. I'd counter that run-time has more information to work with, which allows for more choice in what level to work at.

      I also don't think that code generators that write ugly incomprehensible code are a good thing. Ugly code generally runs poorly. If it's tedious code, then maybe that's another case, but it shoudn't be ugly. Even tedious code probably means that some refactoring would improve performance.

      In the end, the code that gets run should end up the same, whether the opcodes are chosen at run-time or compile-time. So the question is which allows more flexibility, easier programming, and better performance?

      As far as parsing, I think the trend is actually toward run-time, with XML. Basically, you have a choice between a parser library, with callback routines, or something like YACC or ANTLR with code fragments. (Would you use YACC to parse XML?) Personally, I think that the main reason we've stuck with YACC for so long is that that's the way we've always done things. That, plus (premature) optimization in thinking that compiled code is always faster.

      Again, we're making the same compiler vs. interpreter trade-off arguments. So I suppose that if you've chosen C/C++ because of those trade-offs then compile-time code generation is probably the obvious choice. If you're using Perl or Python, then you're probably going to parse things at run-time. Java probably falls somewhere in the middle. Personally, I've been heading more toward preferring Python.

      --
      Software sucks. Open Source sucks less.
  94. CodeCharge by Anonymous Coward · · Score: 0

    Another code-generation tool is CodeCharge. It generates database-driven web apps.

  95. Me own project... by 3seas · · Score: 1

    I see the review and read some of the links, other links are just to slow ATM....

    But doing my own general automation tool set

    to be updated this weekend

  96. No mention at all of MDA? by eduardodude · · Score: 1

    Surprising. Check out http://codegeneration.net/ , one of many sites on this OMG standard.

  97. I think this is the definitive book on the subject by Anonymous Coward · · Score: 0
  98. Effective Code Generators by Anonymous Coward · · Score: 0

    There are examples of code generators that will save you heaps of time. Similar to compilers, they often take the form of translating between languages.

    Java to XML
    http://www.castor.org/

    Java to Relational Database
    http://jaxor.sf.net

  99. Skeptical about code generation by Tablizer · · Score: 1

    Here is a link on c2.com with a skeptical debate about code generation:

    http://www.c2.com/cgi/wiki?CodeGenerationIsaDesi gn Smell

    Basically, it is saying that code generation is converting info from a compact, well-factored, form to a less compact less well-factored form. In some cases I can see where it may improve run speed, but not code maintanance. One should generally remove duplication of patterns from code and data, not add to it.

  100. No hable LISP by Anonymous Coward · · Score: 0
    I am curious if LISP's output (thinking in terms of your precomputed tables example) is required every compilation or if it can be treated as an object file and just linked. I don't want to have the added time of a "preprocessor on steroids" every single time I compile... but I sure would enjoy having an actual generated (pre-runtime) table in case I need to change scope, range, or granularity (like in 3D rotation and transformation tables at different ranges from the viewer).

    Even better, since I wish to put the power into the hands of the designer, I would like to have dynamically (but again pre-runtime) generated tables and other "static" cheat-sheets without EVER having to compile. Think world/level/rule design tool suite. Basically I want to avoid the runtime script interpretation or even file reads into memory... saving as much time as possible (even if using the "preload" level trick at the beginning) while retaining as late a binding as possible.

    Is this an unreasonable request?

    1. Re:No hable LISP by Anonymous Coward · · Score: 0

      Sadly, coming from a 45-year old tradition largely separate from more mainstream languages, Lisp has a somewhat skewed view of data types and program composition. In practice this means that Lisp is easiest to work with when it's in charge of the environment, although you can call "foreign" libraries via standard APIs relatively easily. The relative popularity of Lisp being what it is, there has been a lot of work to ease integration with mainstream languages, although you still need to do some extra work to define the "foreign" interfaces. For the best integration with mainstream languages, look up Xanalys Lispworks (commercial), Allegro Common Lisp (commercial, very expensive), or maybe the free ECLS or GCL (don't be impressed by the GNU moniker).

  101. building a bridge by Anonymous Coward · · Score: 0
    perhaps the biggest problem (going along with what you are saying) is that many don't actually understand the importance or processes in engineering. NO, I am NOT saying or implying "policy." No, I am not referring to specific textbook processes with multiple hyphenated names and fancy logos.

    What I am talking about is the act of planning ahead but understanding the need to adapt. Take the military for example. History has shown that it was not just a matter of technological empowerment but ability to adapt that defined sucessful armies (and leaders). Take the best weapons in the world but no plan, direction, adaptability, or process of fighting and eventually the peasants will defeat you. The peasants here can be seen as competitors... or just flaws and lack of consumer interest in shelling out dough to you. In feudal Japan the wise Daimyo's understood that there should not be a confusion of statescraft and warcraft. War is expensive and the statesmen that is concerned with fighting must understand that up front. He must also stand back after dictating his desires and let the general do his job... don't micromanage especially in things you are not adept in.

    Foolish marketroids and suits are those who set schedules for things they don't understand. Scott Adams has had a field-day with these PHB's. Engineering is the process of creation... without process you will at best have a bridge that is 10 times over budget, 3 times pastdue and only fails 20% of the time. This is not even considering maintenance as if the bridge were built like most software then the architect and all the workers would be put to death without ever having written down the plans and building methods (much less materials). This worked great for giant stone pyramids where maintenance was not an option (you built it then moved on) but a bridge is actively USED and thus will need to be maintained.

    Bill Gates made his infamous comparison of the auto industry to computers in terms of innovation and advancement. He was rightly ridiculed for this even if the majority of flamming idiots didn't realize (or care to discover) the real comments and what they really were about. However, if the auto industry really was conducted like the software industry then you can bet that there would be a lot more Pinto-like vehicles around today and a lot fewer drivers.

    Oh, one thing more... isn't it funny (not haha... well maybe it is) how the IT industry has such a hypocritical attitude towards innovation and problem solving? Shouldn't a consumer be a BIT concerned when the developers not only do not use their own system in house for their needs but do not apply even the most rudimentary elements of the underlying philosphy behind that implementation in their daily development activities? I recently worked for an organization (but yet that implies being organized which it never was) that built a collaboration suite. Not only was this not used in house to collaborate the development and other business needs throughout, but in fact the whole idea of collaboration was as foreign to them as if you had asked Hitler to head up the Organization for the Advancement of Jews, Polish and Russians.

  102. Darn Toot'n... much like game reviews by Anonymous Coward · · Score: 0
    Yay! It's "Analogy Time"

    One thing that is evident from reading reviews from any entertainment media is that most reviewers and even most paid reviewers simply do not understand WHAT MAKES A GOOD REVIEW. Lets say I can't stand first person shooters. They make me dizzy, are too twitch oriented and frankly are embarrasingly stupid as far as "realism" goes. If some game comes out that is first person but in no way is a FPS then a comparison by some "cool dude" idiot reviewer (using phrases like "mad skills" and other monkey speak) to being like Quake in some ways is going to turn me off. However, that is a good thing! If it were just a review of it being so awesome and giving some weak examples of liking the gameplay... that doesn't help me make an informed decision. I want to know the reviewers slant. I want to know WHAT the gameplay is like. What the goal of the game is and what to expect from playing it.

    I want to say, "Well if I liked that type of game it is apparent from these reviews that I would most likely enjoy this particular title... however as I don't like that type I will move on." While the content (opinion) of the review is obviously subjective you at least provide what your criteria were.

    Now imagine people using this method to judge and review political situations. You JUST MIGHT end up with less monkeys playing "My team is better than your team" crap.

  103. Java to generate Java? by Fellgus · · Score: 1
    ... 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 ...., but with the majority of the code-generation examples being for Java-related technologies, I wonder why Java was not selected instead.

    It all about choosing the right tool for the job. Arguing that you should use Java to generate Java is like asking a carpenter to use a board of wood as the only tool to make a table.

    Using Ruby (or, Perl, etc.) makes the code generator a lot simpler.

    --

    -larsch

  104. Is This Better than EOF? by TheInternet · · Score: 1

    Okay, so I read the article, and the concepts sounds interesting. The thing I'm struggling with is that I read about how laborious it is to write the same type of EJB database access code over and over, and the solution is to have the code generated for you. I may be missing something, but I think NeXT solved this problem quite a while ago with Enterprise Objects Framework.

    So, yes, you could write code to write code, but I'd personally prefer having less custom code to begin with. At least in the case of database access, EOF does just that, and I'm not clear what advantages code generation hold over the EOF approach in this scenario. Can anyone familiar with both approach shed light on this?

    - Scott

    --
    Scott Stevenson
    Tree House Ideas
    1. Re:Is This Better than EOF? by mhanlon · · Score: 1

      I was thinking exactly the same thing while reading the article.

      EOF and, for your desktop apps, Interface Builder, the GUI builder that doesn't generate code, it just maintains an object graph...

      Unfortunately, programming for NeXT/OpenStep ruins you for pretty much all environments after that...

      --
      _m
  105. Chapter titles might be fun by ZorroXXX · · Score: 1
    Examining chapter titles can be quite fun actually. For instance in the table of contents to the book Pair Programming Illuminated you find the following gem:

    ``My Partner Is a Total Loser'' and Other Excess Ego Problems

    --
    When you are sure of something, you probably are wrong (search for "Unskilled and Unaware of It").
  106. Generative Programming by beef3k · · Score: 1

    If you enjoyed this book then you should probably have a look at Generative Programming by Eisenecker and Czarnecki as well.

  107. Well, that didnt' scare me yet... by Anonymous Coward · · Score: 0
    I will look into it. I am looking into solutions for the "field of abstractions" especially that done runtime with lookups seeming to go in circles until the correct function is found. The idea of a well abstracted environment is great for good design, quick development and putting more power into the hands of designers... but the overhead and security issues are not fun at all.

    Thanks