With JavaScript metaprogramming is possible

Metaprogramming is the writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at run time during compile time. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually. The Pragmatic Programmer describes metaprogramming as pulling the details out of your code and into metadata. Often this metadata comes in the form of a configuration file or some other data source, but there is no reason it can’t be executable code, as long as you enforce a clean separation of the “what” and the “how”. The idea with this approach is to write your code with a vocabulary that mirrors your problem domain (the “what” you’re trying to describe). This kind of mini-language is where we get the term “domain specific language”, or DSL.

Now, in our case, the details - or the “what” - are the actual rules for when to show and hide form fields. Right now we have the rules intertwined with the implementation of those rules, and we have felt the pain from it - pain that will only continue to worsen as requirements change and features are added. What would be great is if we could pull those rules out so we can modify them independently of the implementation. What would be great is if those rules were written in a language that looks more like our problem domain than JavaScript.

Adam McCrea gave a presentation at the Columbus Ruby Brigade meeting about metaprogramming JavaScript. He posted the presentation’s slides here (pdf download). Do not miss his recent tutorial on subject