One of the obstacles that programmers encounter when developing with different programming languages is that each language has its own "culture" and ways of getting things done. Sometimes different instances of the same language can suffer from this malady. This adds a complexity to software development that would be a waste of time if it weren't so necessary.
In the C++ programming language (where I spend the other half of my programming life), a solution to this kind of problem has been developed, standardized and wholeheartedly adopted by developers worldwide. It's called the Standard Template Library or STL for short.
A bit about the C++ STL
The STL offers a package of useful collections, iterators and algorithms based on a small and consistent set of rules. Different parts of the STL are interchangeable and the interface for each object is consistent, which makes it easy for programmers to switch between different parts or even implementations without having to modify other parts of their code.
An important aspect of the STL is that the runtime behavior
of each collection, iterator and algorithm has also been standardized and documented.
For example: The time to insert a value into a STL map is documented as O(log(N)) which is a favorable
behaviour. Inserting a value into a vector anywhere but at the end is an expensive
operation which takes time O(N); in the worst case
scenario, nearly all of the elements in the vector will need to be shifted.
Templates were added in C++ to add a bit of flexibility to the strongly typed nature of the language. It allows code to be written in general terms without specifying directly the types of values being operated on. The following example demonstrates the use of templates to determine the maximum of two inputs:
return left < right ? right : left;
var i = max(-5,7); // i becomes 7
var f = max(5.4,4.5); // f becomes 5.4
var s = max("hello","world"); // s becomes "world"
The use of templates in C++ extends to more than just functions of simple types. Templates may be used to define new classes as well as functions and these may be based on other classes and even combinations of multiple classes and simple types. Take a look at the following definition:
template <class T, int M>
bool fn(const T &left, const T &right)
return (left.getValue() % M) < (right.getValue() % M);
this.M = M;
MyLess.prototype.fn = function(left,right)
return (left.getValue() % this.M) < (right.getValue() % this.M);
Function overloading in C++ allows multiple functions sharing the same name to be defined. The compiler knows which version to call depending on the types of arguments passed to the function:
void F(int i)
// do something with an integer
void F(string s)
// do something with a string
F(5); // calls F(int)
F("hello"); // calls F(string)
if ( typeof(v) == "string" ) // do something with a string
else // do something with an integer
This one function serves the same purpose as the pair of overloaded C++ functions above. Each time it is called, it tests whether the argument is a string or not and decides what to do with it afterwards.
Created: March 27, 2003
Revised: August 22, 2005