« The Beaufort scale of cruft | Main | The fish have started to reappear »

September 30, 2002

Andrei Alexandrescu, Modern C++ Design

Most good computer books enhance your understanding -- of theory, of practice, of process, whatever.  It's rare to find one that transforms your understanding.

Alexandrescu's thesis is simple: that modern C++ is expressive enough to be used as a design language instead of an implementation language.  His first example, policies, illustrates how the language enables you to express design choices -- for example, whether to create objects via operator new or from prototypes, or how a smart pointer should manage ownership or thread-safety -- as template parameters.

This in itself is a pretty dramatic step forward for C++, but Alexandrescu then combines it with the concept of typelists, a way for code to talk about types themselves instead of just object instances.  Armed with policies and typelists, Alexandrescu goes on to provide generic implementations of design patterns such as Visitor and Abstract Factory.

Think about this for a minute.  With Alexandrescu's techniques, you can express a design pattern directly in code.  It's no longer a matter of recognising the pattern and manually implementing the code for it.  Once you've made the design decision -- for example to use Abstract Factory -- you can express it directly in the code via Alexandrescu's AbstractFactory<> template.  And because AbstractFactory<> is parameterised by the list of types and the creation policy, you can equally easily express the design decisions of which types the factory creates and how it creates them.  The language allows you to directly express these design decisions instead of implementing them.

This, for me, is a transformational change.  I'm used to C++ as a language for expressing low-level choices.  I'm used to vocabularies for expressing design choices at a documentation level.  What's new is the way Alexandrescu's C++ gives us a vocabulary for expressing and executing design choices directly at the code level.

In the interests of realism, I should point out that Alexandrescu uses advanced language features that are currently poorly supported by some popular compilers.  (Oh, all right, let's name names: the Microsoft compiler isn't up to the job.  To be fair, Microsoft's C++ team have said that compiling the Alexandrescu library is one of their top priorities: but for the time being, if you're using Visual C++, you're out of luck.)  But this is almost beside the point.  Compliance will come, and pioneering yet practical work like Alexandrescu's is exactly the incentive the compiler vendors need: who wants to be the only vendor who can't handle AbstractFactory<>?

In any case, this would be worth reading even if no compiler were ever to implement the parts of the standard Alexandrescu needs.  It is worth reading whether or not you are interested in C++, just like Structure and Interpretation of Computer Programs is worth reading whether or not you're interested in Lisp, or Object-Oriented Software Construction is worth reading whether or not you're interested in Eiffel.  For the most part, it's not about libraries or C++; it's about the issues of writing programs at the level of design rather than implementation.  (There are some bits about how to abuse the C++ compiler to achieve design-level goals, but these are relatively few and far between.)  It is a manifesto as much as a cookbook, and it's certainly made me think about the expressiveness of code in a whole new way.

September 30, 2002 in Books, Software | Permalink


TrackBack URL for this entry:

Listed below are links to weblogs that reference Andrei Alexandrescu, Modern C++ Design: