2012-04-03

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.
 
Yes, it's an image of text.  Just like a binary file is an 'image' of something that could have been easy to manipulate.
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.

9 comments:

Anonymous said...

Text +1

The Art of Unix Programming has an entire chapter devoted to this (protocols, file formats, configuration files etc).

Jim L said...

While I agree with the point of the post, it was juxtaposed on Hacker News with the excellent presentation on Unicode, Love Hotels and Unicode (it's pretty standard until about halfway through - then it gets interesting fast). Because of that lucky coincidence I am now thinking, "This is all fine and dandy, but what, exactly, is 'text'?" :)

axilmar said...

No, text is the wrong medium of communication. What we should have is a simple binary format that allows us to express the fundamental data structures (8/16/32/64-bit integers, 32/64-bit floats, 8/16/32/bit characters, records, arrays, references). Writing an editing tool for such a simple format would be just as easy as a text editor, but our files would have structure and be much more compact at the same time.

Unknown said...

See also http://www.cryptonomicon.com/beginning.html, especially the section on 'Metaphor Shear'.

Stephan Wehner said...

Another example: Open Office odt files, which are compressed but easily expanded with unzip. Then one gets to look at the files making up the document, for example: Configurations2/ content.xml META-INF/ meta.xml mimetype settings.xml styles.xml Thumbnails/

Stephan

Anonymous said...

try ASN.1 - it exists for that exact purpose.

Anonymous said...

try ASN.1 - it exists for this exact purpose.

MCAndre said...

....yep. Binary formats are impediments in day-to-day tasks and in furthering technology for tomorrow.

phonokraft said...

Yes, text files really make these things easy to work with! Thanks for the examples!

It is also very interesting to think about the efficiency. To represent a color in binary might take, say, 3 single precision floating point numbers, of 4 bytes each. That would be 12 bytes.

Let's compare this to the dictionary approach:

Color

b
0.147909
g
0.237903
r
0.0450865


Without any indenting, saving this file on my machine uses 159 bytes. This approach uses 13 times as much storage (or bandwidth) as the floating point approach. Of course, compression mitigates this somewhat, as you note.

And yet...

On an individual use case, one might say this doesn't matter.

But if one considered the energy consumed by servers that store the world's data, an efficiency factor of 13 might make a difference. Which would you choose: 13 megawatts, or 1 megawatt? $13 million, or $1 million? Etc.

Recently, I haven't been able to stop thinking about this sort of issue as it applies to global energy use. I wish I could, but I can't make the thoughts go away!

Well, just a few thoughts.