Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by Ada in 1983, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication. Software entities created using generic programming are known as generics in Ada, Eiffel, Java, C#, and Visual Basic .NET; parametric polymorphism in ML, Scala (possibly the only modern language that supports both parameterized types originating from functional languages and virtual types from the OO paradigm) and Haskell (the Haskell community also uses the term "generic" for a related but somewhat different concept); templates in C++; and parameterized types in the influential 1994 book Design Patterns. The authors of Design Patterns note that this technique, especially when combined with delegation, is very powerful but that "Dynamic, highly parameterized software is harder to understand than more static software." (Gang of Four 1995:21)
Generic programming refers to features of certain statically typed programming languages that allow some code to effectively refine the static type system. For instance in C++, a template is a routine in which some parameters are qualified by a type variable. Since code generation in C++ depends on concrete types, the template is specialized for each combination of argument types that occur at some instantiation. Generic programming is used to implement the STL libraries to define containers, iterators, and algorithms. Examples of containers are vectors, queues, sets, maps, and lists and hash tables and functions such as a particular sorting algorithm for objects specified in terms more general than a concrete type.
Generic programming pioneer Alexander Stepanov wrote: "Generic programming is about abstracting and classifying algorithms and data structures. It gets its inspiration from Knuth and not from type theory. Its goal is the incremental construction of systematic catalogs of useful, efficient and abstract algorithms and data structures. Such an undertaking is still a dream."( p. 17). Following Stepanov, Bjarne Stroustrup defined generic programming without mentioning language features: "[l]ift algorithms and data structures from concrete examples to their most general and abstract form."( p. 35).
Full article ▸