A Comfortable Couch

Monday, December 13, 2004

STL Gripe

Anyone who's ever used the C++ STL will confirm that it is extremely hard to use for a beginner. It's non-intuitive to use, it's impossible to read, and the compiler doesn't tell you in anything resembling useful terms what you got wrong. Of course, things get better as you become experienced, but using the STL is way harder than it should be.

The problem with the STL isn't the implementation of the STL, the problem is C++. The C++ template rules are so arcane and specific, I thinks its amazing that someone was able to put it together at all (although I think the C++ template rules were in part influenced by STL's designer Alexander Stepanov to give him the features necessary to build it).

No, the problem is that the template rules are just too difficult to grok easily. So when you are attempting to use one of the container classes and you don't correctly implement the necessary operator overloads (which is beyond the understanding of most developers already) there will be some bizarre compiler error. The errors it gives always are something like "Can't use l-operand within const expression with habeas corpus and grits". And it's REALLY difficult to look at the template source and figure out the actual problem. Looking in the header file at the code the compiler barfed on usually is something like this:

...
while (!_Isnil(_Trynode))
{ // look for leaf to insert before (_Addleft) or after
_Wherenode = _Trynode;
_Addleft = this->comp(this->_Kfn(_Val), _Key(_Trynode));
_Trynode = _Addleft ? _Left(_Trynode) : _Right(_Trynode);
}
And then you're like Moe Syzlak "WHAAAAA....." and you spend the next 2 hours trying to figure out exactly what you got wrong. When this happens to me I usually start looking for example code on the net and hope I can see what it is I'm missing.

It doesn't have to be this hard. Well, as long as you use C++ it does, but there are plenty of other languages, particularly dynamic languages where the equivalent of generics is much easier. For the vast majority of development, C++ just plain sucks. Use something else whenever possible, not just for yourself also but for the poor schlub who must work on this stuff after you.

1 Comments:

Anonymous said...

Yeah, that "habeas corpus and grits" thing gets to me every time! That's why I use Python when I can...

--Ned.

1:18 PMlink  

Post a Comment

<< Home