April 22, 2013

Thoughts on literate programming

12:50 -0400

At work, I've been implementing a data structure to make our collaborative editor run quickly. As part of that work, I've had to write a couple of complex functions (a couple 200+ line functions), which got me thinking about comments, readability, and presentation.

If you've never heard of literate programming, it's an idea introduced by Knuth (surely you've heard of him) that combines programming with documentation intended for human consumption. The program is presented in a document written for people to read, and transformed by a program into something a computer can execute. (The Wikipedia article on literate programming gives a decent description.)

I've dabbled a bit with literate programming in the past. In fact, I'm the maintainer for the noweb package in Debian. One of my (very) long-term projects is to build a free data structure library written for people to learn how the data structures work, and I've started implementing a couple simple data structures in literate programming style. However, looking at literate programming again, it seems to me that it has a few deep limitations.

First of all, if you want to describe something in depth, you're forcing everyone to read it, even if they aren't interested. For example, in the wc example, “#include <stdio.h>” takes 3 lines, even though anyone who has read an introductory C programming book will know immediately why that's there. On the other hand, you might want to include that for beginner programmers. One of the frustrating things I found when writing research papers was that I often had to go into too much detail, to make sure that every single step was covered, which I felt sometimes turned a short, simple proof into something unwieldy. What I would have liked to do was something like Leslie Lamport's (of LATEX fame) hierarchical proofs (though it doesn't translate well to printed text, and needs a more dynamic medium like a web page).

This limitation is partially due to the time that literate programming was conceived. With printed text, either you write something and everyone sees it (even if they just skim it, it's still there for them to see), or you omit it and nobody sees it. With something like a web page, however, you don't have this limitation. You can write “#include <stdio.h>”, and hide the descriptive text unless the reader wants to learn more.

Another limitation that I find with literate programming is that one of its underlying implications is that code is a lesser way of communicating between people, and that people communicate best using natural language. Each code chunk is intended to be described in words. While natural language is the best tool for general human communication, a small chunk of well-written code, like well-written mathematical notation, can be very effective in communicating certain ideas. Literate programming would encourage you to write the chunk twice, once is code and once in natural language, even if the code is a sufficient (or even sometimes better) way of communicating the idea. Going back to the stdio.h example, just writing “#include <stdio.h> // we send formatted output to stdout and stderr” would be a sufficient description for most programmers.

Related to this, literate programming pulls code chunks out of context, which sometimes is an important part in understanding how the code works. Seeing the code in context gives clues about what state the computer is in before it is executed, and what is expected after it executes. Of course you can always describe that in text, but seeing the code in context sometimes gives experienced programmers a more intuitive feel for how the code works.

One thing that I like about literate programming, though, is that emphasizes understanding over a line-by-line presentation. For example if you have two chunks of code that operate on the same data (say one reads and the other writes), or if you have two chunks that have operate similarly, then you would write those together, instead of having them spread out according to how the computer would execute them. It also allows you to deal with more important or interesting parts first, and leave the more mundane parts for later (I would have put “#include <stdio.h>” near the end of the document).

It is also useful to have at your disposal some of the document-writing tools, such as sectioning, lists, mathematical equations, and beautifully formatted text (and not having to make sure that your lines are wrapped properly).

While I think that literate programming is a great idea for presenting code in an understandable manner, I think that it has a lot of room for improvement, especially if we can take advantage of some of the features of the web. I'm doing some experimentation, and I hope to have some positive results.

0 Comments
December 21, 2008

If programming languages were religions

21:21 -0500
[thumbnail]
URL:
http://www.aegisub.net/2008/12/if-programming-languages-were-religions.html
Tags:
technical, programming, humour

In a long tradition of comparing some aspect of computers with some other aspect of real life comes: "If programming languages were religions". There is one flaw with this article, though: programming languages are religions.

0 Comments
May 30, 2008

Jabber bots

18:31 -0400

OK, this is very old news, but anyways... I have written a couple of Jabber bots. The first one is very simple system monitor, which basically allows me to keep an eye on my server(s) from my Jabber client. It is in the public domain (but depends on software that is GPLed).

The second is more interesting: a weather monitor. It gives me the current conditions of various cities, and I can ask it for the forecast. Currently, it just fetches the data from Yahoo! weather, but I'm hoping to add other weather services soon. This one is released under GPLv2 or later.

0 Comments
April 21, 2008

Summer: a time for coding.

18:51 -0400

My application for Google's Summer of Code 2008 has been accepted. Basically, this means that Google will be paying me to program for the summer. (It also means that I better have my thesis more-or-less done soon, so that I'll have time to work on it.) Guess which tan line I will have at the end of the summer.

According to the official timeline, I'm supposed to start working on May 26. I'll probably do some setup and background work before then, though.

While I had been interested in Google's Summer of Code since it began, my school schedule (grad school at Waterloo means no summer break) prevented me from participating. This year, though, it seems like perfect timing for me, since I should be finishing up most of my work on my Ph.D. soon, but won't have a job yet (anyone want to hire me?).

The project that I'll be working on is improving GNUstep's integration with other desktop environment. GNUstep is an implementation of the OpenStep API, which is what is used by Apple's MacOS X. GNUstep doesn't (yet) have the glitz of MacOS X (although hopefully that will change, maybe with the aid of another GSoC project) but it is a very powerful and flexible system.

One of the problems of GNUstep, though, is that it doesn't quite cooperate as well with other *nix-based desktops, such as GNOME or KDE as it should. freedesktop.org was created to create standards and software for the *nix-based desktops so that they would all be able to cooperate. My project will focus on getting GNUstep to adhere to as many of the standards as I can.

A preliminary list of the ones that I'll be looking at (in approximate order that I'll be working on them) is:

  • there are some window focusing issues that need to be fixed
  • EWMH hints
  • XDND
  • startup notification
  • XEmbed client support (so that GNUstep windows that are embedded in other apps follow the XEmbed spec — XEmbed embedder support would probably go in an extension)
  • System Tray (requires XEmbed client support)
  • gstreamer (for NSMovie, NSSound?, and related classes)
  • avahi (implementing Apple's NSNetService API)
  • .desktop file support (reading, and improved writing)
  • icon naming (so that GNUstep can share icon themes with other desktop environments)

Of course, this list is subject to change. There are a lot of other standards that I could work on. It will all depend on discussions with my mentor.

Stay tuned for more updates.

0 Comments
December 4, 2004

Asymptote

22:44 -0500

I have created a new Debian package for Asymptote, a “script-based vector graphics language inspired by MetaPost.” Hopefully, this will become my second package to be included in Debian.

I haven’t used Asymptote much (because I’m not in the process of writing any documents right now), but it looks pretty interesting. I’m wondering if this could be the answer to the pain of drawing finite automata and graphs. Doing graphics with scripts seems a bit hard to wrap your head around, unless you have a very good visual memory. But drawing finite automata is already painful.

Besides, one of the authors taught me second-year calculus, and his office was across from my own when I worked at PINTS. And he helped our group a lot with our DEC Alpha machine.

0 Comments