## Tuesday, April 03, 2012

### In praise of... text files and protocols

The other night I had to debug a problem where CMYK colors specified in an OmniGraffle file weren't making it into an exported PDF (or at least appeared not to be). At first it looked like it might be a nightmare because what I really wanted to do was ignore the OmniGraffle UI and look inside the .graffle file and the PDF itself. But salvation was at hand: both .graffle and PDF are text formats. The OmniGraffle file is actually an XML document (in some cases it's a gzipped XML document but it can be decompressed with gunzip). Here, for example is part of the Colors.graffle file that's provided as a sample. It's easy to see the RGB colors that are specified and just as easy to modify them by adjusting the text file.

While fiddling around in the .graffle file looking at the CMYK colors I spotted that some straight lines that had been drawn in OmniGraffle were not quite straight. That's quite tricky to see in the UI, but dead easy in the XML document and you can simply fix the coordinates. Here, for example, are segments of a line drawn from the Nucleobases.graffle sample file:

The text format made it easy to examine what was happening under the hood of the fancy UI, to quickly fix small problems and to manipulate the file using other programs. Similarly, once I'd determined that in the file I was working with the CMYK colors were fine I exported a PDF and decompressed the result using pdftk. It was fairly easy to follow a color specification through from the .graffle file and into the PDF. Here, for example, is an RGB color specified in the Nucleobases.graffle and the corresponding color appearing in the exported PDF of the same file:

And with that I was able to determine that the CMYK colors were correct and that any problem lay with the person I was sending the PDF to.

The deeper story is that human-readable text formats are wonderful: they are easily debugged, they are easily manipulated (with text editors and other tools like awk and sed), and they can be compressed using common compression programs if space is a problem.

Similarly text based protocols (such as HTTP, IMAP, SMTP, FTP and POP3) make it easy for humans to write, read and debug. One of the things that made POPFile easy to implement was that all the mail protocols are text based (the entire POP3 proxying module is able to use simple string matching and regular expressions to handle POP3). And they are also line oriented (a command is read by reading to the line ending). That makes programs to handle them very easy to implement.

Recently I used an undocumented API that was entirely text-based (using JSON) to obtain live bus arrival times in London and make an Ambient Bus Arrival Monitor.

Another great example of a text format appears in the code that's behind Hacker News and UseTheSource.  In the Lisp philosophy your program is also data it can consume and the data about users is simply sent to a file as Arc code meaning that any admin tasks that don't (yet) have UI can be performed trivially by hand:

Of course, the downside is that text takes up extra space and for low-level protocols (such as IP) it makes sense to use binary. But for almost everything else it's best to use text. Only use binary protocols where the performance is so sensitive that it's worth the implementation and debugging downside. The upside is that no special tools are needed.

I wonder how much of the success of the Internet can be put down to the decision to use text-based protocols for almost everything that people will need to implement.  And how much we owe the early writes of the RFCs in deciding that text was best.

PS A reader points to Eric Raymond's Art of Unix Programming and specifically the chapter called Textuality.

PPS A commenter over at Hacker News makes the very good point that it's easy to version/diff text files and very hard with binary.

PPPS Another commenter over at Hacker News points out that there's a chapter in The Pragmatic Programmer called The Power of Plain Text.

If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: The Geek Atlas. Signed copies of The Geek Atlas are available.

<$BlogCommentBody$>

<$BlogCommentDateTime$> <$BlogCommentDeleteIcon$>

<$BlogBacklinkControl$> <$BlogBacklinkTitle$> <$BlogBacklinkDeleteIcon$>
<$BlogBacklinkSnippet$>