Generic programming is a powerful paradigm to separate data representation from algorithmic and functional aspects and leads to universally useable software.

Generic Programming

Rail signals © HPW Fotolia.de

The paradigm of generic programming has gained increasing popularity following the publication of the C++ Standard Template Library (STL) in 1995 and its subsequent incorporation into the C++ standard in 1998. Due to the improved support for generic programming in the new standard C++11, the practical importance of generic programming will continue to grow.

What is it?

Generic programming decouples algorithmic logic from the representation of data, while maintaining efficiency of the resulting programs. For instance, the STL deals with sequences (arrays, lists, queues) and algorithms on sequences (searching, sorting, permuting). If, for example, we want to find the maximal element in such a sequence, it does not matter, from an abstract point of view, whether that sequence is an array, list or if it is even read in value by value from a file.

Using a traditional implementation method, for each of these variants a separate function has to be created, and if we want to find the minimal element, yet another set of functions is needed. The STL in contrast defines a number of suitable abstractions (here, so-called iterators) capturing the essential computational properties of sequences. Using C++ templates, one can support all these different data structures with a single implementation, corresponding to the linear search algorithm. If a more efficient algorithm exists for sequences with more powerful properties, the generic implementation is specialized to use the more efficient version if applicable (for instance, use binary search instead of linear search on a sorted array).

What's the big deal?

This approach avoids a combinatorial explosion of different but essentially identical implementations, each tied to a specific data representation. Thus, reuse and productivity can be raised considerably. Adobe already successfully uses generic programming. Sean Parent of Adobe estimates that up to 85% of source code can be eliminated by using this method.

Today, a number of generic implementations for many different application domains are available. One example with a particular connection to CMM is my Grid Algorithms Library GrAL, a library for geometric algorithms and data structures (meshes), which offers support for the parallel implementation of such algorithms.

Indeed, the generic approach gains particular traction by the current trends in hardware development towards parallel and heterogeneous architectures (think multi-core, GPU computing etc.). Performance-critical software has to exploit modern hardware to stay competitive. On the other hand, there is a substantial risk to tie software design to a specific hardware architecture, for instance by strong coupling with vendor- or hardware-specific parallel interfaces. Generic programming offers interesting solutions to this dilemma by implementing structurally equivalent patterns with a single generic parallel component. This is the idea behind libraries like Intel's Threading Building Blocks.

Fast train © lassedesignen Fotolia.de

How does it work?

More information can be found on my detailed pages on generic programming, including a motivation of generic programming, a definition of this paradigm, an a growing tutorial. Here, you'll learn generic programming from the ground up, following the evolution of a simple example from C-style to a fully generic component in C++, and working around a number of pitfalls. If you like to learn more, I've collected available ressources and discuss some exemplary generic software.

If you like to learn how to use generic programming for your company or institution, please consider my training offers.