Sunday, November 18, 2012

Software building blocks

I've changed the background image on the blog; it shows the street pavement outside a small hotel in Milan, where my wife and I stayed some years ago. The rectangular blocks are arranged to form overlapping arcs, a nice contrast between the view close-up and from a short distance.

One highlight of our trip was a visit to the Milan cathedral. It was under construction and most of the exterior was covered for protection, but we were able to wander across the lower rooftops and admire the stonework and detailed carvings, high above the street level.

I sometimes use the phrase "building blocks" when I talk about programs. It's just a metaphor, but it conveys a few useful intuitions. Building a structure (or even a road surface) means choosing blocks that fit together well; programs need to fit each other if they're to work together. (Though "fit" should be taken in a more abstract sense; it has do with the details of information representation and communication rather than size and shape.) Many kinds of architectural structures are only as sound as their foundations; similarly, complex software often depends on a set of reliable, generally useful programs for common tasks--Windows applications have been developed using Microsoft Foundation Classes for the past two decades. Architecture has distinctive styles, and the same can be said of programs.

In 1977, Christopher Alexander and colleagues published A Pattern Language, a landmark book on architecture. Alexander observed that the environments we construct hold patterns, and some of these patterns work very well over time; they can be captured in a language to describe and inform architectural design, from single rooms to entire cities. "A pattern is a careful description of a perennial solution to a recurring problem within a building context, describing one of the configurations which brings life to a building." Here's an example:
Buildings, and especially houses, with a graceful transition between the street and the inside, are more tranquil than those which open directly off the street. 
Make a transition space between the street and the front door. Bring the path which connects street and entrance through this transition space, and mark it with a change of light, a change of sound, a change of direction, a change of surface, a change of level, perhaps by gateways which make a change of enclosure, and above all with a change of view.
Alexander's work has been influential in software engineering, in the concept of software design patterns. Often software engineers encounter variations on a problem, and they design their solutions to have similar structure across the different variations. 

Imagine designing an information management system that allows users to generate summaries and make changes and additions to the information. One simple design pattern you might adopt is the Model-View-Controller, or MVC. 

Roughly speaking, you separate the model (the information and the rules you have for managing it) from the view (the procedures for generating presentations of the information) and the controller (what the user is allowed to do with the information). In principle this gives your system flexibility: for example, you can design different views for different kinds of users, and you can provide different controllers for users who might have different levels of authorization for manipulating the information. There are subtleties I won't go into, but the larger message is that, like Alexander, software engineers can think about the systems they build in much more abstract terms than the behavior of individual lines of code.

When I was working in industry, I had a poster of the Cologne Cathedral on my cubicle wall. Below is a snapshot I took in the 1980s, scanned into digital form. My poster showed a drawing of the cathedral before it was completed--construction began in 1248 and wasn't finished until 1880. Building software sometimes feels like that. Eventually the system will be finished, but it can be hard to tell when. Choosing the right building blocks is important, as is putting them together in the right patterns.

No comments:

Post a Comment