Wednesday, January 31, 2007

XML Kvetching

Dominique Boucher has a nice little article on why he hates XML for DSLs. I, as most people who know me can imagine, am right there with him on all his points.

I'll even add my two cents as to why I don't like using XML for DSLs. I don't XML because it forces you to develop your own language from scratch.

Sure, you're file format starts off simple. But before you know it, you want conditional blocks, and the ability to include and merge files.

My preference is is to embed an already existing language rather than invent one with XML. Why? Because the language will have already solved a bunch of problems that you'll have to tackle eventually with XML. This includes basic things, like a parser for your language, to more sophisticated things, like data hiding.

One of my favorite languages for embedding is Beanshell (I love using Scheme too, but that's another blog post for another day.

With Beanshell's ability to have predefined functions, I can write code that looks like a pretty high level language. As a bongus, I get all the power of Java, has support for conditionals, functions, etc. that I would need to add to XML.

The usual argument against this approach is that Beanshell (or Java) is hard for end users to tweak, while XML is easy. Hogwash. Tell me if either of these seem hard or easy to tweak?

if(isProduction()) {
  logger = new RemoteLogger();
} else {
  logger = new LocalLogger();
}
 <host-configuration>
   <host type='production'>
    <set property='logger' value='RemoteLogger'>
   </host>
   <host type='default'>
    <set property='logger' value='LocalLogger'>
   </host>
 </host-configuration>

I like the first approach because it's a real language - with variables, functions and the ability to express abstractions. If you grow your own language from XML you're only left with two choices: live with a half baked language, or prove out Greenspuns's 10th Rule.

No comments:

Post a Comment