Wednesday, January 17, 2007

Dynamically Generated Code

One of the things I don't get is why dynamically-generated code is supposed to be such a big deal. The experience of writing code to generate code should be extremely familiar to anybody who's ever had to make a non-trivially complex Web app, and even to many people who've only made trivial ones. I've personally always avoided PHP like the plague, so the vast majority of Web apps I've built, especially during the bubble, used Perl to generate HTML and JavaScript. Some trivial scripts used Python to generate only HTML, some used JSP inline HTML within the context of larger frameworks, some used other systems or other languages, sometimes the JavaScript was always the same, sometimes it changed from page load to page load, yadda yadda yadda -- the point is, in every case, the fundamental experience of building Web apps is writing code to generate other code. Even the simplest PHP script will have conditional display of particular bits of HTML.

The current fervor for metaprogramming ultimately traces back to this. It's possible that it could even be the cause of Java's apparently-impending demise. I think generating code from code is harder in Java than in any other language I really know. If you cut your teeth writing CGI scripts which conditionally generate various bits and pieces of HTML, it's really pretty much inevitable that when you mature as a programmer, you'll gravitate to objects whose methods are defined at runtime, higher-order functions, and what the Pythonistas call "monkey-patching". I know I'm not the only person who turned aside from a "creative" career to learn programming at the start of the bubble ("creative" in the corporate sense -- obviously, in reality, programming is a very creative career). You've got an entire generation of programmers who see their work as essentially the art of constructing code from code.

Anyway, this line of thinking is a response to these:

Digging Into Ruby Symbols
How Ruby Sucks (by Matz!)

The first contains, as an example, what has got to be the single most obvious, most lucid "hello world" of Ruby metaprogramming I've ever seen. The second was a presentation at RubyConf in 2003, and covers changes that have happened since then, as well as changes still yet to come.

Enjoy.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.