On Thu, 6 Mar 2008, harshal wrote:

> Can anybody share the experiences with meta-programming (for example,
> in writing DSLs.) so that if possible we can come up with best
> practices?
> It would be great if somebody is already aware of such best practices.

Before STL was widely available... I wrote a C++ library sort of
equivalent to STL.

At first I used templates quite heavily.

After awhile I realised I had no idea really WTF C++ was doing when it
instantiated those templates.

So I ripped all the C++ template code out of the library and replaced
it with marked up code that a perl script ate.

The script spat out plain, well formatted vanilla template free C++
code.

MUCH BETTER!! SO MUCH CLEARER! SO MUCH EASIER TO DEBUG! SO MUCH EASIER
FOR CLIENT CODE TO USE, READ AND REUSE!

But before I could become famous...

STL came along...

Bugger.

Lessons learnt :-

* Meta program sucks. You (and your user) have no idea what you have
   really generated.

   So generate intermediate files of vanilla ruby strings, which you
   can output to file, print, inspect at leisure, understand, test,
   debug and eval.

* Mark up languages universally suck (yaml, xml, html, sgml,
   ...).

   Unless you need a vendor neutral language to communicate with
   another application, with strong schema validation to prove who has
   got it wrong... Don't use markup languages!

   Ruby is expressive enough, and it's schema is better known / understood.

* Instantiating code should instantiate the documentation as
   well. Reading templated code is hard, reading templated code with
   generic documentation bends the mind.

* Perl sucks. Ruby is way better.

* The guy sitting next door to Matz is going to beat you. (The authors
   of STL worked just down the corridor to Strostrup and convinced him
   to tweak C++ templates into something usable. :-)


Whilst we're on the subject...

I once wrote a Domain Specific Language. Full flex / bison lexer
parser + byte code interpreter.

Lessons learnt...

   * No language I can write in a man year or ten will have any where
     near the power and expressivity of Ruby.

   * No language I can write in a man year or ten will be as debugged as Ruby.

   * No language I can write in a man year or ten will be as well documented/exampled as Ruby.

   * Any program sufficiently large will contain a poorly implemented
     version of Common Lisp.

   * I wish I had Ruby at that time. If I were to redo that project I would...

   - Provide a framework of ruby classes to do the hard domain specific stuff.

   - And (maybe, if non-techies really really had to read / write this
     stuff) provide the very thinnest layer of syntax sweetening...

   + that translated directly into vanilla ruby intermediate files..
   + that invoked the framework..
   + but permitted use of the full power of Ruby for most of the work.




John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter / tait.co.nz
New Zealand