Archive for April, 2011

Programmer Religious War – C vs C++ Style Casts.

There are many issues in the programming world that seem to divide people down religious lines. These issues seem to cause endless internet flame wars and programmer rants without ever coming to a consensus.

Fight

Creative Commons License photo credit: Philippe Put

One of these issues I’ve been seeing a lot lately is about whether to use the old school C style casting, or use the specific C++ style casts.

Here at Riot C++ style casts are part of our coding standard and the use of C style casts is generally discouraged in our codebase. This is a pretty common standard, as seen even in the Google Style Guide.  But, conversely you have several major game studios with the inverse standard, who ban any use of C++ style casts and allow only C style casts. I’ve spoken with excellent programmers at places like Blizzard and Insomniac who say they will personally never use C++ style casting in their game code.

Isn’t it quite strange that multiple AAA studios can have completely opposite coding standards on this topic? At first I thought, like with a lot of C++ features, this was a console studio vs PC studio type of issue, but from my conversations with colleagues, I dont even think this is the case anymore. It seems to simply be a matter of style and personal taste.

C++ Style Casting

syntax: int myInt = static_cast<int>(myFloat);

For a detailed description of C++ style casting check out this paper on the subject. C++ broke up casting into several different keywords with different use cases, here I will take you through a super-quick overview of them:

static_cast: The most common C++ cast. Casts between like types, with some compile-time checking.

dynamic_cast: Casting with runtime type checking. Checks to make sure a cast between a parent and a child class is valid, and can return NULL or throw an exception if it is not.

const_cast: Used for adding or removing the const keyword from a type.

reinterpret_cast: Used to cast between any two types, even between incompatible or opaque types.

There are many advantages to using C++ style casts over C style, including:

1) The intent of the programmer is more explicit. Since there are different keywords for different types of casts, it is easies to understand what the programmer wants to accomplish. For example, when you see a reinterpret_cast, you know the programmer is intentionally casting between incompatible types for a specific reason.

2) C++ style casts are safer. The most common cast that replaces C style casts is the static_cast. If you try to static_cast between incompatible types, the compiler will warn you, whereas it will not with a C style cast. For example, if you have the forward declaration of a class, but forgot to include the complete definition, and perform a cast to that type, then a C++ static_cast will complain at compile time, however a C style cast will compile fine and  most likely crash at runtime.

3) C++ style casts are greppable. It is easy to do a find in files to find all instances of a C++ style cast such as const_cast in your codebase. However, this is much more difficult with C style casts.

4) dynamic_cast provides runtime type checking, while a C style cast can not. Most games have RTTI disabled for performance reasons, so this one isnt as interesting, but this feature is still occasionally useful in tools code.

C Style Casting

syntax: int myInt = (float)myFloat;

C Style casting is the one that most people grew up with and are most familiar with.  Put some parenthesis around the type you want to cast to and away you go. So why do so many people stick with this old school way when C++ has eliminated the need for it?

1) The C style syntax is much easier to read. The C++ style casts takes the short form of the C style, then adds a verbose keyword, and angled brackets, making it much uglier and harder to quickly parse. It is very important for programmer’s to be able to read code quickly, and easily get an understanding for what is happening at a glance. Many people feel that the janky syntax of C++ casts obfuscate the code when compared to the more elegant C style casting syntax.

2) Everybody is already familiar with C style casting. It is widely used and understood, and when writing code using them you can almost guarantee that any other C/C++ programmer recognizes it and understands that. C++ style casting is just not as widely used, making it so that your code is not as universally understood by other programmers.

3) Since the C++ casts are more complex than normal ones there is more potential for programmer’s to not fully understand them and potentially misuse them. Simply using static_cast for example can sometimes change the value of a pointer, and as one programmer I know expressed: “I  never use any language construct that requires moving the ‘this’ pointer behind my back.”

4) dynamic_cast will slow down your game because it uses RTTI. In addition to being an expensive operation, relying on dynamic_cast means that your code is probably badly designed and very brittle.

My Stance

At Riot we are particularly partial to the use of C++ style casts, and I fall squarely on that side of the fence as well, however I agree completely that dynamic_casts are bad news in runtime code. Like many studios we have RTTI disabled, so the option to use them is not even there.

This is, however, an unlikely place for me to fall on this issue. I typically am a “readability above all” type of programmer, and the arguments that C++ style casts make code uglier and harder to read at a glance ring true with me. However, I feel that the added safety of C++ style casting and the explicit intent they communicate much outweigh this downside and make them a welcome addition to any C++ codebase I am working in.

How do you handle casting in your codebase?

 

Post to Twitter

5 Book Recommendations for Preparing for Your Game Programming Interview

I received a ton of responses from all of you job hunters out there to my last post on resume tips. Most of the questions I received was from people wanting to know how to prepare for the next phase of the process and be able to do well on their interview.

There is already a ton of general interview prep help out there, so I wanted to give you guys something a little more specific to game development. I also wanted to give you guys a little insight on how I prep for interview. I am kind of a technical book maniac; even when on vacation, lounging on the beach you will find me with a stack of programming books. And yeah I am talking about actual BOOKS, not just websites and Google. Google is a great resource, but for real learning there is nothing like the physicality and singular purpose of a book when trying to force your focus onto new concepts.

So following are my top 5 book recommendations for preparing for that game programming interview. Not only have I used these books myself when preparing for an interview, but many of the questions I ask when interviewing come from these. They cover the basic fundamentals that  a game company will expect you to know.

With these recommendations, you really don’t need to read each one cover to cover. Look at each one as a menu you can sample from. Flip around, read what piques your interest, and mark chapters to come back to for further study.

I want to mention a few common book recommendations that did not make my list. These are Design Patterns and Code Complete. These are great books, and working in the industry you will hear them mentioned alot, but I dont really feel they really offer much practical material that will be specifically helpful on an interview, especially when studying under a short time frame. In addition, while these books are great classics, which also means that some of their material is a bit dated now.

1) C++ Coding Standards  by Herb Sutter

2) Effective C++ by Scott Meyer

C++ is probably the single most essential and commonly covered skill in game programming interviews. Riot is a little more C++ focused than many game companies since we are so PC focused, but you will find C++ language questions a staple across the industry and you better make sure you are ready to tackle them confidently.

C++ Coding Standards” and “Effective C++” are hands down the best C++ books available. Their style of short, enumerated topics, each only a few pages long make them extremely easy to read and very friendly to skimming. Be aware that both of these books rely heavily on parts of the standard library that companies like Riot use, but many game companies out there do not use. Even so, however, the concepts still apply. If you don’t have time for anything else, at least read these books!

3) The Pragmatic Programmer by David Thomas

This is perhaps my favorite programming book of all time.  Its funny, its practical, and it sometimes goes against the grain of “standard” programming advice you may have had drilled into you at school. This book is a great look at many of the common every day issues that come up on a programming team, it will give you the vocabulary and talking points you need when discussing general engineering discipline during your interview.

4) 3d Math Primer for Graphics and Game Development by Fletcher Dunn

When walking into a game programming interview you’d better know your dot products from your cross products, because basic 3d geometry and linear algebra is a staple of almost every interview I’ve seen. Mr. Dunn’s book is a perfect refresher for the exact things you will be expected to know. Certainly there are much better math textbooks out there, that cover this stuff in much greater detail. But, the great thing about “3d Math Primer” is that its not a textbook. The information it covers is extremely practical, its very readable, and its actually pretty funny (not something you can say for many math books!). While the book has a focus on graphics, don’t overlook if just because this happens to not be your specialty, most of the material in the book applies very directly to general game development that employers expect you to know even if you are not a graphics programmer.

5) Data Structures and Algorithms for Game Developers by Allen Sherrod

I feel weird recommending this book, as its a little beginner focused and honestly, it has a lot of very questionable C++ code in it. There are a lot of awesome textbooks on the subject which are just a lot higher quality and go into great detail on the subject. But that exact weakness is also the strength of the book  in a cramming situation, it focuses on the high level parts that you need to know. A book like this is perfect for quickly getting back up to speed on your basics of which sorting algorithms are O(N^2) and which are O(n log n).

As far as data structures are concerned, at Riot we use the Standard Template Library (STL), and you should familiarize yourself with it’s basic data structures. Even though many game companies never touch STL (especially console-oriented studios), often their internal implementations are heavily influenced by the STL interface, so its very useful to be familiar with it.

Post to Twitter