10 comments

[ 8.0 ms ] story [ 24.1 ms ] thread
Doom 3 was the first game Id did in C++ instead of C, which helps explain a lot of the coding conventions.

From Carmack himself[1]:

In some ways, I still think the Quake 3 code is cleaner, as a final evolution of my C style, rather than the first iteration of my C++ style, but it may be more of a factor of the smaller total line count, or the fact that I haven’t really looked at it in a decade. I do think "good C++" is better than "good C" from a readability standpoint, all other things being equal.

I sort of meandered into C++ with Doom 3 – I was an experienced C programmer with OOP background from NeXT’s Objective-C, so I just started writing C++ without any proper study of usage and idiom. In retrospect, I very much wish I had read Effective C++ and some other material. A couple of the other programmers had prior C++ experience, but they mostly followed the stylistic choices I set.

I mistrusted templates for many years, and still use them with restraint, but I eventually decided I liked strong typing more than I disliked weird code in headers. The debate on STL is still ongoing here at Id, and gets a little spirited. Back when Doom 3 was started, using STL was almost certainly not a good call, but reasonable arguments can be made for it today, even in games.

I am a full const nazi nowadays, and I chide any programmer that doesn’t const every variable and parameter that can be.

The major evolution that is still going on for me is towards a more functional programming style, which involves unlearning a lot of old habits, and backing away from some OOP directions.

[1] Originally as a comment on https://www.kotaku.com.au/2013/01/the-exceptional-beauty-of-... , but it seems to be missing now.

Side note, I love C# in general but it'd be nice to have something the like const methods in C++.

Honestly, he's not a great C++ programmer. I think he'd agree that he's learning, and got away with C for far longer than normal just by being a phenomenal programmer in general.

IIRC the Doom 3 renderer was originally written in C and he ported to C++ to match the rest of the codebase.

This statement caught my eye. In what ways can tell that someone is a great programmer without them being good in a particular language? Can someone be a phenomenal programmer without being well versed in any particular language?
A couple famous examples are Dan Bernsteins's code style is considered weird (he was trying to write safe code his way in C) and Monty's MySQL code is considered "ugly" but runs great.
It sure doesn't look like C++ programming.
Even an article as shallow as this raises some red flags about the code base:

* A common base class is rarely a good pattern in C++. Such classes have a tendency to bloat.

* A custom string implementation is a cliché for new C++ adopters. Everybody thinks they need to reimplement std::string. Most are wrong.

* The code as shown separates declaration and initialization of local variables, which decreases readability and safety of the code.

Yeah, not sure why the author was praising the excellent library and string class. That’s what std provides. Why reinvent the wheel?
Because the year is 2000-2002 and you are embarking on writing a game engine targeting PC, Mac, Xbox, Playstation, and any other consoles or platforms that come onto the market before your engine is finished. Compilers and standards compliance was a shit show. Things like EA-STL might look silly nowadays, but there are many great reasons why companies rolled their own stuff back in the day.
Even now STL is not perfect for realtime and threaded programming.

Thus comes from outdated and schizophrenic approach to memory management. It is getting fixed but that will take years and multiple revisions of standard.

Let's start with lack of immutable objects, continue with missing support for pmr or allocators and finish with hidden default heap allocations in algorithms and data structures. (Including some smart pointers.)