Using the right tool at the right moment is the best way to increase productivity. Those who have a thorough knowledge of the different tools in existence (with their key functions and major weaknesses clearly identified) can prove this sentence true. The purpose of this article is to give a brief outline of what C++ is capable to do as a tool, following its recent standardisation.
I wrote this article way back on 25 March 1999, after having joined DCDM Consulting as a Business Analysts, fresh from my Computer Science studies in France, and having had the pleasure and experience of writing a major software in C++ during an internship in a startup called Technodigit in Lyon. Some parts of the article are outdated but most of its content is still relevant after 23 years…
C++ as a programming language
The C++ programming languages was devised by Bjarne Stroustrup in the early eighties as a better C. C++ has a clearer syntax and, as a result of it being object-oriented, was much more adapted to handle complexity.
With time, vendors including Microsoft and Imprise (formerly Borland) customised the language for their needs. In parallel, Bjarne Stroustrup and his colleagues at Bell Labs introduced new features in the language like exception handling, templates and namespaces. As a result, C++ became more complex and therefore more confusing to the programmers. At the beginning of the 90’s, C++ was already a highly powerful tool but so complex that very few people mastered it.
The need for Standard C++
The American National Standards Institution (ANSI) and the International Standards Organisation (ISO) decided therefore to set up a committee to work on a Standard C++ project, with the ideas of purifying the language while adding essential but overlooked features to it. Bjarne Stroustrup quickly joined the committee together with Alexander Stepanov and Meng Lee, destined to become heroes of the C++ community.
The Standard Template Library
Alexander Stepanov and Meng Lee introduced the Standard Template Library (STL) to Standard C++.
STL has a dual personality: it is both a container library (with facilities to handle dynamic arrays, linked lists, trees and so on) and it enables one to program using generic and functional paradigms.
A new way of thinking: containers and iterators
STL defines different kinds of containers such as dynamic arrays, lists and trees together with numerous operations acting on them (like adding and deleting elements). One of the major breakthroughs of STL is the fact that the complexities of these different operations are known and are invariant. Adding an element to the head of a list always takes constant time whatever the size of the list. The programmer can therefore calculate the complexity of his own algorithm based on the published complexities of these STL operations.
The elements of a container are accessed via iterators. These special objects mimic C++ pointers. An algorithm to read all elements of a container must do the following: set an iterator to point to the first element and access the other elements by incrementing the iterator until the end of the container is reached…
When the OO, generic and functional worlds meet: generic algorithms
… which brings us nicely to genericity. Given that all STL containers are accessed through iterators, generic algorithms can be devised which work with all containers (whether dynamic arrays, lists, trees, etc.). The complexity of a generic algorithm depends, of course, on the container on which it is being used. The current version of STL has about 80 generic algorithms including functions such as find, fill, randon-shuffle, sort and reverse among others.
If the programmer decides to implement a new container (e.g. b-trees) having the same semantics of an STL container, all 80 generic algorithms will immediately work as expected thereby greatly increasing productivity by cutting development time. In the same way, writing a new algorithm (e.g. topological_sort) respecting the principle of genericity will enable it work with all STL existing containers.
Function objects
Another breakthrough in STL is to treat functions as objects. They can be passed as arguments to other function objects and can be composed with other function objects. Furthermore, they can be used as higher-order function objets (that is, functions returning functions as result). We can say that well-written STL function objets as just like mathematical functions. Well-written in this context means that the function objects does not have side effects.
This feature enables the functional paradigm to be used in C++. Specifically, composition of function objects can be used as a technique to handle complexity. Each function object (whether written by the programmer or not) can be considered as a component and these can be glued together to solve computing problems. A central repository of function objects can be set up and every time a component having certain behaviours is needed the programmer can check if an equivalent function object exists in this repository. Electronic or civil engineers have used this technique for many years and it is high time that computer engineers start to use it. The immediate benefit is a decrease in development time.
The Way Forward
Standard C++ is a major advancement over C++ just like C++ was over C. Owing to STL, component-based programming is becoming possible. Thinking in terms of containers, generic algorithms and components enables the programmer to concentrate on what is really important for the client: having a working solution for his problem that respects requirements and which is delivered on time. The big question is therefore whether the programming community is ready to embrace this new technology.
In the near future, it is estimated that this leading-edge technology will enable customisation and development of programmes to take up to 40% less time implying faster customer service at lower costs.
Leave a Reply