Tuesday, 27 November 2007

The Four Stooges of Standard C++

The C++ standard library is more than just std::vector<T,Allocator>. It is a rich set of data structures (containers) and algorithms that are functional in nature. At the very least, the library allows you to use higher-order functions to perform common tasks on the various containers. This is accomplished through functions such as std::for_each, std::transform and many others (scroll to the section on algorithms).

Why would you use the standard algorithms? Glad you asked. In my opinion, the main reason is readability. The above mentioned algorithms were the ones that came to mind immediately because they are the ones I've seen used the most. The aha moment for me (and indeed, I believe anyone that I've mentored) is that you realize that the algorithms communicate intent and not instructions. As we all know, declarative languages are so much better than imperative languages (except no one uses them.) But especially when it comes to data structure manipulation, being able to read what is going on rather than how it is being accomplished is great for readability. My intent is not to convince you of this fact, but just know that this is the main benefit for me.

Unfortunately, there isn't as much use of these algorithms as there should be. On the bright side, through the Boost libraries, people are being exposed (albeit indirectly) to their use more and more. This gives birth to four types of programmers or the Four Stooges (purely affectionate term):

  1. Eager beaver

  2. Wise old fart

  3. Scaredy-cat

Yes, I know there are three here :-)

The eager beaver is the (likely junior) programmer who must use a stdlib algorithm no matter how convoluted it appears. He has read The C++ Standard Library three or four times and can more or less flip to the exact page he needs. This is a very good programmer to have. Eventually he will gain experience when to use them, when not to use them, and how to really use std::remove_if, becoming quite pragmatic.

The wise old fart is the (likely senior) programmer who has been programming in C++ back when it was called CFront. He still thinks that the C++ standard library is unportable because of the time that HP changed that one thing with the IOStreams and his program crashed because of it (I made that up.) But, he is usually open to other people using it and doesn't chide them for doing so. He makes sure that whoever is using it, knows what he is doing though. This is a very good programmer to have. Eventually, he will start using the algorithms and because of his experience, will be very pragmatic.

The scaredy-cat is very difficult to root out. Usually, he appears to be disguised as the wise old fart: experienced, appears to be deliberate, questions what the other programmers are doing, among other generally paranoid activities. The dead giveaway though is that there is not one use of the C++ standard algorithms or data structures in his code. He will stick to arrays and for loops. This programmer is good to have because of his experience, but he writes potentially buggy code. An example of potentially buggy code is code that were you to resize a container to contain one less element, that would cause a buffer overrun later. No, you shouldn't "have known that would happen." That is why containers that know their size exist. I don't know how you deal with this guy. If he is very stubborn, it is a tough situation.

The first two stooges can eventually become the fourth stooge: the pragmatic programmer. This programmer does not magically appear but always an evolution of the first two stooges. He is the super stooge. He understands that sometimes you just need a for loop.

No comments: