Just A Summary

Piers Cawley Practices Punditry

DSLs, Fluent Interfaces, and how to tell the difference

Posted by Piers Cawley Thu, 15 Mar 2007 10:39:00 GMT

I’m getting heartily fed up of people banging on about Domain Specific Languages. It seems that every time someone writes a Ruby library that uses class methods, symbols and hashes reasonably sensibly they get delusions of grandeur and call the result a Domain Specific Language (or maybe an ‘embedded’ DSL).

In a sense, they’re right, but it’s a pretty compromised language simply because you’re stuck with the Ruby parser. Scheme and Lisp hackers probably look at (say) ActiveRecord and sneer. Heck, even Perl programmers have grounds for getting their sneer on

Now, before any Ruby programmers go getting on their high horse about Perl programmers being indisciplined louts, may I refer you to Getopt::Euclid, an alternative to Perl’s Getopt::Long library.

Getopt, or something like it, is pretty much a universal among programming languages. It’s the library that makes it ‘easy’ to write commandline programs with unix style switches. It’s often one of those functions that ends up taking an ugly argument string which defines all the possible flags your command could have. As interfaces go, it’s often actively user hostile – the argument string is a DSL, but it’s one that took its design cues from the notorious sendmail.cf.

So, Damian Conway fixed it. To use Getopt::Euclid you just import the library and then write your command’s documentation using Perl’s inline POD with a couple of extra Euclidean extensions and you’re done. Getopt::Euclid treats your documentation as its specification and builds its option parser from that.

Now that’s what I call a DSL. Entirely embedded in the Domain Specific (natural) Language of documenting a command line program.

Damian’s a genius at this sort of thing. Check out List::Maker, where he finds a little used part of perl’s syntax and wedges in a bunch of cunning ways of building lists, including something that looks remarkably like list comprehensions.

And this is in Perl 5; the version of the language that doesn’t have explicity support for syntax modification.

Another example of the kind of thing that’s possible without monkeying with the parser is the stuff Jifty (originally JFDI), in particular the way Jifty Schemas work.

What’s my point?

I’m not saying don’t take the time to make your interfaces ‘language like’. However, there’s a lot to be learned from the way other languages have approached the idea of the DSL or ‘little’ language. Implementing something like Jifty’s Schema’s in Perl is far from easy (though the techniques needed are getting better understood all the time) and involve ferreting around in dusty corners of an already arcane syntax, but the beauty of getting it right is that you simply don’t have to care about how its implemented. The neat bit, the bit that’s worth pinching is the syntax of the resulting DSLs…

Oh yes, and, while you’re about it, take a look at what Why the Lucky Stiff is doing with hpricot, definitely one of those libraries that goes out of its way to make life easy for its users.

The Fine Art of Complexity Management

Posted by Piers Cawley Fri, 01 Aug 2003 08:44:00 GMT

Picture this: A magician sits in front of you with a pack of cards in his hands. He turns over the top card, it’s the Two of Hearts. He has you sign it. He then turns the card back over then takes the top card from the deck and pushes it home somewhere in the middle. He asks you to snap your fingers, then he turns over the top card of the deck again. It’s your signed Two of Hearts.

What I’ve described is the opening of one of the classics of card magic, called The Ambitious Card, in which a signed, selected card repeatedly jumps to the top of the deck in ever more implausible conditions. On the face of it, the effect is simple, the card just keeps jumping to the top of the deck. Behind the scenes, the method is simple too; you just have to execute a hidden move so that it looks like you’re really doing what you claim to be doing. And there’s the rub. Though I say so myself, I can execute the required move with no little skill, but if I stop to think of the complexities of what I’m doing, I won’t be able to do it well. I certainly can’t explain how it’s done to another magician, well, I can, but the explanation goes along the lines of “Just practice ’til it looks like the real thing”.

What does this have to do with complexity management? Well, apart from the fact that a good magic trick should be presented in such a way that, no matter how complex the method, the audience just sees the magic, obviously. But there I go again, using the tool we all instinctively use to manage complexity. That tool is the word ‘just’. We all do it, we push the complexities of something behind what I’ve taken to calling a ‘Just Story’. Here’s a just story for sole meunière:

The fish, fried in butter, is transferred to a serving dish and over it is poured a quantity of freshly cooked, hissing, foaming butter. A squeeze of lemon juice, a scrap of parsley, and the dish is ready.

Couldn’t be simpler, could it? Well… Elizabeth David, in French Provincial Cookery goes on to explain the full complexity of the dish so:



Just A Summary