Niclas Nilsson home

Don’t program in XML

I hope you enjoyed the C# Application Markup Language article as much as I did? I found it very amusing at least. :-)

The “scary” part is that even though Petzold was joking, there are (almost) such things. Jelly is one example, but I’m sure there are others. Jelly of course looks much more pleasant than the fictive C# Application Markup Language, but to me, it is still a flawed idea.

I admit, I once (briefly) thought that I wanted a programming language with XML syntax, and when I read the blog post Jimmy Nilsson wrote about More language focus, I got proof that I wasn’t alone. I’m sure there are a bunch of us out there (and some people obviously went further and built it).

Executable XML is fine, as long as it isn’t your programming language. You do not want your source code to be XML, since XML is not human readable enough. Sure, it is easier to read than machine language, but not nearly as easy to read as the programming languages you use today and the ones you will use tomorrow. (If I’m wrong, you should seriously look around for another language).

Easy to parse, but…

I was looking for a way to add scripting capabilities to an application and since XML is very easy to parse, I thought I wanted a programming language in XML. Luckily, I realized I was wrong.

There are several much nicer alternative solutions to this problem. One of easiest is to add a scripting engine to your application. It is often very few lines of code that is needed to add such an engine, which makes the return on investment very good. I can recommend Jython (Python on Java) since I’ve used it and liked it, but I’ve heard good things about JRuby (Ruby on Java) as well, and on the .NET-platform, IronPython is now available (and Jim Hugunin, creator of both IronPython and Jython, works at Microsoft nowadays).

If you use a scripting engine, the good thing is that you don’t have to invent a language, you don’t have to write a parser and you can point your users (the script writers) to a book they can buy in the bookstore. And as a bonus, you only have to write documentation about your application specific API:s they need to know about. The drawback of using a general purpose language for scripting your application is that it may be overly complex for your users. In that case, there are other solutions (for instance, Domain Specific Languages) that I will write about further on.

If you really want executable XML, consider generating that executable XML from something else. Something that is more readable and maintainable for a human being.

Have a good time with your scripting engines!