My personal transformation about templates

I’d like to take upon myself the challenge of writing a response to the three blog posts that Reg Braithwaite wants to see written. What I’d like to talk about is how I went from never using any templates to using tons of them.

I spent a bunch of time in college programming in C++ but avoiding templates and the STL. I think this was largely because various people were telling me about how much templates sucked and they weren’t portable and whatnot. So I went along with it. And I wrote a bunch of nice little base classes to do various useful things and wasted a bunch of time. And the thing is, the Mozilla folks in their old C++ portability document that got passed around was a perfect, knowing, portable, quotable document to refer to.

There were other things, of course. I really don’t like the way that templates make your code look. And I was in school, where we had to implement all of the fundamental data structures ourselves, so having a huge base of tools already written wasn’t very big in my mind.

I wrote some fairly substantial pieces of code on my own, for my career development, at this time. I even burned a CD to hand to prospective employers at career fairs with some of it. And all of it was totally and utterly template free.

Finally, I graduated, found a job, and entered the real programming world, where things had to be done.

Within a few months, everything I did was STL based.

What caused the turnaround? Well, there’s a few ideas that I had previously held quite dear that I realized were pretty dumb:

  • Portability only gets you so far. See, it might be desirable for Mozilla to work on 100 different platforms, but most of my code has just needed to run on a selected few Unix variants or Windows. Between a modern Unix, Windows, and the Mac, that’s about it!
  • C++ compilers got a LOT better. Your standard base was quite good by 2000 or so across all of the usefuful platforms.
  • STL makes life easy. You tons of algorithms and structures to do stuff, and you won’t feel like cutting corners because you don’t feel like making the right data structure.
  • Some code has to be ugly. Templates and operator overloading make for ugly code. But that’s OK, because you need to really know what you are doing and be ready to get dirty if you want to do it because the subtleties of doing this sort of thing takes a long time to truly grasp. The goal is not for everybody to build happy templates and classes. The goal is for a small group of really talented programmers to build a selection of reusable tools to make everybody else’s life easier.
  • I realized that coding fundamental code from scratch is masturbation for programmers. You are programming, but you aren’t getting anything done.
  • Co-workers brainwash you. Sometimes you just end up doing what everybody else does.