Thursday, June 29, 2006

Getting a games programming job - Tip 3 - Don't rely on STL

STL is a nice library. It allows you to quickly create useful data structures with very little coding. It saves you from writing a lot of code which is conceptually quite simple, but often a source of annoying bugs. It also handles memory allocation for you, so you can have variable sized lists and strings without the hassle of doing a lot of allocation and deallocation of memory.

In game development, many companies make use of STL in tools. It’s reliable and saves development time. Also in tools, we are often less concerned about memory and performance issues. In console game code however, STL is used infrequently.

I’ve met several interview candidates who I would consider overly reliant on STL. When asked to solve a problem which called for an array of non-fixed size, the candidate would not be able to come up with an alternative for an STL vector. Or to give another example, would solve a problem which called for a simple array by using an un-necessarily complex STL type, like a hash.

Being familiar with STL is a good thing. It’s a useful tool when used in the right context. Having the flexibility to use it or cope without it is the best position to be in. I think that a good check for yourself is making a rule that you’re only allowed to use an STL data type once you’ve implemented the structure on your own at least once.


James Sutherland said...

To my mind, STL usage is going to become a lot more common on consoles with the new generation of systems. I like STL containers because they're well-tested, have all the features you're ever likely to need from your container classes, are pretty well designed and generally pretty fast. And also because writing container classes is really dull, and the people who made the STL know more about doing it than I ever want to.

With a decent modern compiler (this may or may not include GCC as I've less experience there, but certainly includes the MS compilers), if you look at the disassembly of the output from code that uses STL containers, it's really surprisingly efficient for the most part. Certainly there were issues on the last generation of consoles, but I'd want a really good reason before I started writing my own vector class on a new system. I'd also argue that rolling your own containers can indirectly lead to less efficient code as you may end up with people using a less appropriate container for whatever they're doing due to there not being exactly the right kind available in their own library. The nice thing about the STL is that there's generally a container for whatever type of work you're doing whereas there typically aren't as many in home-grown solutions.

Certainly STL isn't without problems, in particular when it comes to constraining memory allocations and it being a bit of a nightmare to look at in a debugger, but I think it's often dismissed too easily for in-game use. Treat it like middleware and evaluate and test it then see if you want to spend time and money building your own solution.

Also: hello!

Mark Pope said...

Hi James, good to hear from you :)

Yes, good points.

I could have worded the 'try writing it yourself' part a bit better. I wasn't meaning for people to write all the crazy template code, more to just manually write a vector, linked list, hash table specifically for the system you're writing. It's a comment aimed at junior programmers who might miss out on learning the basics of handling memory and understanding how to write a nice functional container.

John holden said...

Although I am only a computer science student who hasn't even been admitted into the major yet (my school has a pre-major program that you have to pass before they let you take any of the more advanced classes) I can attest to how useful it is to have implemented some of the STL containers. Granted, my implementations are no where near as efficient as the STL. However, now that I have implemented them I understand the STL (and how to use it effectively) much better. For example, having implemented a hash map myself, I better understand the advantages and disadvantages of linear/quadratic probing, chaining and double hashing.

Even better, is that having implemented various containers, I now understand the advantages and disadvantages to various different containers (dynamic arrays vs. heaps vs. self-sorting binary search trees vs. hash maps vs. linked lists).

I am grateful to my professors for making their students implement containers before allowing them to use a standard library equivalent.

Anonymous said...

STL -is- C++. If you're looking for a C++ programmer, then him answering with std::vector is very appropiate.

Mark Pope said...


I understand that in many other areas, including the tools we use to make games, STL is very useful and good to know. My point is that people should not rely upon it, and be capable of doing the basic coding required if STL isn't present. For various reasons, STL is often not used in console games, and not having the versatility to cope without it, makes interview candidates less strong.