Programming

Practical Closures

The problem I see with most introduction to closures is they choose examples that can be done easily with a standard for loop, or some other standard construct, leading a student to ask (with great justification!) what the point is and why (s)he should bother. In my opinion, the best place to start understanding them practically is to look at their ability to decouple logic, because there they have benefits that are difficult or even impossible to replicate without them.

So despite the huge amount of verbiage on the net about closures, I thought I'd take another crack at explaining it with examples that actually do something that is much harder to do without closures.

Power-user Configuration Files Should Be Programs

Every program of any complexity needs to be configured. The traditional answer is to put this in some sort of text file, or something else that is fundamentally static data that must be read in and parsed. (That is, for the purposes of this discussion the Windows Registry is a static text file.)

If you are programming in a language that can take a program in at run-time, like Python, Ruby, or Perl, and you are targeting an advanced audience, the configuration file should actually be a program. In the degenerate case, the programming language can easily look like a configuration file, but then, if you need the full power of programming, it's sitting right there.

Update Dec. 24th: Similar thoughts w.r.t. build tools. I think declaration-based systems tend towards "demoware"; any arbitrary task can be made to look good in the demo of a declarative system, but it seems inevitable that before you can even blink, you need a scripting language. And ultimately, as with ant vs. rake, it's simpler just to pull in the full scripting language that to fall prey to Greenspun's Tenth Rule in your "simpler" declarative language.

NonMockObjects 0.2.0 Released

I've released NonMockObjects 0.2.0. This release adds easy support for obtaining many variants of an object, which allows you to easily test that all combinations of some parameters have a certain invariant. For instance, my Entry objects for this weblog can optionally contain a title, a link for the title, and a summary. The only constraint on that set is that a link affects a title, so you really need a title for the link to make sense, although it is permissible to have a link without a title in the database.

Reverse Compatibility as a Pernicious Software Trap

I'm a little late to this party; only today and to a lesser degree yesterday is my typing speed high enough that I feel I can "afford" a weblog post... and even this otherwise pointless paragraph :-) (My previous post on switching to Dvorak was justified as a typing exercise and easily took over two hours to type. This one did too in rough draft but you see how much better I'm doing :-) )