pmuellr is Patrick Mueller, Senior Node Engineer at NodeSource.

other pmuellr thangs: home page, twitter, flickr, github

Sunday, September 24, 2006

Just Say No to XML - more often

In Just Say No to XML, Allen Holub writes "XML is perhaps the worst programming language ever conceived." What's he's talking about here are all the tools which use XML as a programming language. My only direct experience with this is with Ant. Within Eclipse. Because it's the easiest easy way to do 'scripting' with Eclipse. You know it's there, it's shipped with the base Eclipse. And I'll just say that, in general, I'm in agreement with Allen; I've written a shed-load of Ant scripts over the years, and I'd rather not have to be dealing with XML here.

Titanic cake

But I'd like to go even further! Allen follows up his 'worst' sentence (quoted above), with this: "I'm not talking about XML as a data-description language, which was its original design." If we're going to start thinking about not using XML for programming languages, let's also consider not using it for data-description as well. Why? Because XML isn't designed for data. XML is designed for documents.

And there's the problem. Documents. Programmers know how to design data. They don't know how to design documents. I think this is probably one of the biggest problems with the ws-* flavored web services; you need to design documents. To design an XML document, you need to know XML Schema. I know exactly one human who claims to be able to write XML schema off the top of their head, with no multi-hundred page reference document open. That's not good.

Sure, there are ways around having to learn schema, kind of. There's RELAX NG. But it's not widely supported in a least the places I program, and look at that last updated date on the page I just linked to. Kinda scary.

There's design tools, like UML-based stuff, but that adds layers of cake; now I have additional tooling I have to learn, there's code-gen in play so I have to watch for getting things synchronized, etc.

But let's pretend for a minute like there was an easy and standard way to design documents, and that programmers actually knew how to design documents.

Now you got the problem that there isn't really a simple way to go from documents to data. You might want to design your documents to be 'easy to parse' (eg self-descriptive), but that's likely going to violate some of the things you want in your document design, like readability. You'll probably end up using some coolio XML-to-data framework in your own code. However, some folks aren't going to have access to whatever coolio document-parsing-to-data system you're using, since most of these frameworks are single language stories, and your XML may be read by nearly any programming language known to man. In the end, some folks are going to have to use a DOM-based parser, or *gulp* SAX-based parser, to read your data. And that's simply no fun.

What's the alternative? Whatever is appropriate.

Java .properties files / windows .ini files are a really simple format for storing really simple data; too simple in most cases, but not all. Use these wherever you can get away with them.

YAML is kinda like .properties files on steroids.

JSON is the latest contender here, and is an appropriate format to use if you're expecting some JavaScript code to be handling your data, since you can use eval() to deserialize a JSON string into objects. IFF you can trust the source that gave you the JSON, anyway (that's a pretty big If, BTW; there are JSON parsers in JavaScript you can use if you can't trust your source).

Upon first really looking into JSON, I was struck by the similiarity in function that SDO provides. SDO provides a lot more than what what JSON provides, but at it's core, there's some definite similiarities. That seems good.

The point is, XML is unavoidable to programmers these days. It's everywhere. Just don't assume it's the best answer to your problem, just because everyone else is using it. There might be a better answer.

Photo "Titanic cake", with a nice CC license, by psb_minimal. Found using the Create Commons Search Site.

No comments: