Container Models

A long time ago a friend of mine pointed out a sign of trouble in any program: defining a new container model. If you are working in a language that provides containers in the standard library, then use them. If you need a container with specific behaviour, then make it look like the standard containers. If you come across a program which introduces a new model, beware.

I’ve recently been adding Go support to SWIG. SWIG can be used to build interfaces between various different languages and C++. SWIG is a nice program in many ways: it provides a lot of power and flexibility in defining the interface. Internally, though, big chunks are written in C++ but it uses a totally different container model. I assume this is for historical reasons dating back to a beginning in C or possibly even Python. In C++, though, it’s a mess. There is no type checking: it’s all void * pointers. Practically every type (strings, lists, hash tables) converts to everything else implicitly. It brings all the weaknesses of a pure dynamically typed language into C++, without providing any of the benefits.

Sometimes one encounters a programmer who carries a container model from program to program, rewriting it into different languages as needed. The result is a program written for one person. Don’t do that. Use a language in the idioms of the language; don’t try to make it look like a different language. Use the containers which the language provides.


Posted

in

by

Tags:

Comments

4 responses to “Container Models”

  1. manuelsimoni Avatar

    I have a slightly different theory:

    If your data fits well into standard containers, use them.

    However, if you need an extremely (space or speed) efficient way to store data, go ahead and create a new container, and don’t care about making it look like a standard container, except when this is very easy.

    An example would be a very specialized tree for IP address lookup, for which the language’s containers aren’t adequate — for argument’s sake. Since you only use such a specialized container in very stylized and few ways, making such a specialized container look like a standard container may not be worth it.

    In that case, don’t worry, and simply create a very specialized container interface, and don’t make it look like a standard container.

    Makes sense?

  2. Ian Lance Taylor Avatar

    manuelsimoni: Yes, that makes sense. There is nothing wrong with using special purpose code for special purposes.

  3. tromey Avatar

    My rule of thumb for all programming is that boring is better.

    Whenever I see some exciting new data structure or algorithm or language choice or optimization, I start from a position of skepticism. Usually you don’t need this stuff, usually just the most typical thing in your current environment will be best.

Leave a Reply