Software design done right

Apr 18, 2006 15:51

Periodically, I have another go at using LaTeX to see if it's still as ghastly as I'd remembered. So far, my current attempt has gone on for longer than any of the others, and I haven't yet run screaming back to plain TeX. I'm starting to notice the things LaTeX does right: the label/ref macros are nice (though having to run LaTeX twice to get references right is totally bogus), \[ and \] give text editors more of a hint than $$...$$, automatic table-of-contents generation is shiny, the idea of environments is a fundamentally good one, etc, etc. But LaTeX is still evil. I could mention specific cases and get picked apart by those of you with more knowledge (the obvious one seems to be the whole fragile/robust mess - I know it's a consequence of writing things on top of a macro-expansion system, but the default is still the wrong thing. Protect fragile things by default, don't make the user do it: several of the spacing commands have the same problem), but that would miss the point. The real point is that LaTeX is a Language for the Masses, whereas TeX is a Language for Smart People. TeX tries to expand your choices and horizons: LaTeX closes them off, because you're too stupid to handle the power. The documentation simply drips with this attitude: it keeps telling you not to do things, without giving any idea why or what will happen if you do. This may be an acceptable way to speak to small children (actually, I'm not sure that it is), but it's really bloody irritating as a way of talking to TeX users, who are mostly scientists and therefore of reasonably high intelligence. And then, to add injury to insult, it leaves a whole new set of sharp rusty things (fragile commands, for instance) lying about.

Right, rant over. Now I'm going to talk, as promised, about software design done right, as exemplified by BiBTeX and the Unix command units. Both of these are textbook examples of how to write a Unix tool, and actually I'm fairly surprised ESR didn't use them in The Art of Unix Programming. BiBTeX generates bibliographies for your (La)TeX documents. It stores its data in a simple, extensible, human-readable and human-writable textual format (which can then be sent in email, searched with standard tools, etc). References in .bib format are turned into properly-formatted references using a program written in a domain-specific minilanguage. You can add new reference formats without having to recompile BiBTeX. It's generally great. And I see from googling that you can use it with plain TeX, so I won't have to abandon it if/when I shake the dust of LaTeX from my feet.

units is a classic old-school Unix utility: it does one job (converting between different units), but does it really, really well. The standard introduction is to use it to find the number of teaspoons in a cubic parsec (5.96 x 1054, apparently), or 1.21GW/88mph (4.7600819e+10 kg m^3 / s^4, as it turns out) or something similarly frivolous. But it's dead useful. It knows about SI units, imperial units, US units, the different types of barrel used in the wine, beer and spirits trades, any number of physical constants, the boiling points of different types of sugar syrups... All the information about how to convert between them is held in the (human-readable, human-writable, plain text) file units.dat. You can add a new unit, expressing it as a function of any combination of the units it knows already, and units will know about it the next time you run it. It can be called by other programs to do units conversion: I'm 99.99999999% certain that units is the program behind Google Calculator. I was reminded of units and its excellence this morning, when wormwood_pearl read out this entry from Schott's Food and Drink Miscellany:The following is a description of the punch served at a banquet given by Sir Edward Russell, Commander-in-Chief of the British forces, in 1746:

The bowl was the marble basin of a delightful garden, forming the central point of four vast avenues, bordered with orange and lemon trees. A magnificent collation was served on four immense tables, which occupied the length of several avenues. The basin had been filled with four large barrels of brandy, eight barrels of filtered water, twenty-five thousand citrons, eighty pints of lemon juice, thirteen hundredweight of sugar, five pounds of nutmeg, three hundred biscuits and a pipe of Malaga wine. An awning over the basin protected it from rain, which might have disturbed the chemical composition of the delicious beverage: and, in a charming little rose-wood boat, a cabin boy, belonging to the fleet, rowed about on the surface of the punch, ready to serve the joyous company, which numbered more than six thousand persons.
Bloody hell, that's a lot of punch. But how much, exactly? units to the rescue:miles@delirium:~/tex/strictify> units
1948 units, 71 prefixes, 28 functions

You have: 12 brbarrels + 25000*(4/3)*pi*(3cm)^3 + 80 brpint + 13 hundredweight/sugar + 1 winepipe
You want: m^3
* 6.0113098
/ 0.1663531
You have: (12 brbarrels + 25000*(4/3)*pi*(3cm)^3 + 80 brpint + 13 hundredweight/sugar + 1 winepipe) /6000
You want: l
* 1.001885
/ 0.99811857

So, six cubic metres of punch, or just over a litre per person, which seems to be cutting it rather fine if anything. If the bowl was hemispherical, it would have a radius of just over 2m.

I think the boat was probably overkill...

computers, tex, maths

Previous post Next post
Up