views:

314

answers:

8

I'm well educated in the C++ programming language regarding syntax, STL, etc. I haven't done any true projects with it yet other than some college courses. My goal is to start writing progams but attempt to keep it to industry best practices. One of my main goals is to work in the game industry. A door is opening which lead me to ask these questions.

  1. Are C++ game developers usually limited on using all the C++ features? Is it true more or less for each platform?
  2. Is STL used these days in the environment, or should I avoid it?
  3. Do they treat C++ as "everything must be an object" or is it a mix of paradigms?
  4. Any features they tend to not use?
  5. Is C still used, and is it good to show a project completed in it?
  6. I did talk to a famous game designer and he said they don't use STL and said to use basic encapsulation with classes, but he said not to use all other advanced C++ features. The idea is to keep it simple. Is this common?

Thanks in advanced!

+2  A: 

These questions are not at all unique to game development. You will find they vary as much by field as they do by company. I can think of two game design firms that use STL off the top of my head, and also of a few that don't.

1) Define "all C++ features". They typically don't use goto, if that's what you mean.

2) Yes STL is used. You shouldn't avoid it, or not avoid it - use the tools you need to get the job done every time you have a job that needs to get done. Keeping what you use in line with other ways similar problems were tackled -within that project or context- is importent.

3) Depends where you are, both in games and out of games. Some are graduated C guys who want things more C like. Others are C#/Java guys who are more OO (Object Obsessed).

4) goto

5) Yes, and yes. Knowing more than one language is always a good thing. Even if it's just C to C++.

6) Again, use the tools available to solve your problems. Keeping it simple is good when you can, sure.

glowcoder
-1. goto has it's use, especially with multiple directX resource intialization. Much cleaner than trying to catch exceptions, and offers more control than wrapping DirectX object into some kind of smart pointer.
SigTerm
@SigTerm okay, sure it has a use. I'm not going to get in the "goto is evil" debate here. He asked for features they tend not to use, and it certainly is a feature that people tend not to use. The fact that you've found a use for it that you can get away with doesn't mean most people tend to avoid it.
glowcoder
@SigTerm: If you're trying to catch exceptions that come out of resource initializations, chances are you're not making good use of RAII. I started to learn C++ in the early 90ies and have yet to see code that would become cleaner through the use of `goto` (and wouldn't get much more cleaner through the use of other, better features the language provides).
sbi
@sbi: "RAII". Few problems - program should be able to survive failure to allocate a resource (which doesn't cause an exception), it is possible to try to acquire next resource only if previous is available, and resources should be released in certain order. Ways to do that - multi-level if, nested scopes with smart pointers, some kind of class for everything, exceptions or "goto cleanup". goto just happens to be simplest/most compact one, and others are syntaxic sugar on top of "goto cleanup" solution. I've been thinking "goto is bad" until I run into this situation in DirectXFile program.
SigTerm
@SigTerm: It is possible to recover from a thrown exception, it's easy using RAII to acquire resources in order and only if the previous acquisitions were successful, and you usually do want to release these things in reverse order of acquisition. I don't see the need for a `goto` in normal use.
David Thornley
@David Thornley: Yes, it is possible. The problem is that goto is more compact and requires less additional facilities. Also, goto or not goto, resulting behavior will be identical. I see no need to use more complicated solution when simple solution is possible.
SigTerm
@SigTerm: I repeat: I have yet to see code that would become cleaner through the use of `goto` (and wouldn't get much more cleaner through the use of other, better features the language provides).
sbi
@sbi: I'll accept your position only if you can provide a convincing example. Create resource A, use A to make B, use B to make C, make few calls (each can fail with error code without throwing exception), release C, then B, then A. "Convincing" means it will be simpler/shorter than goto solution, and won't need extra additional non-standard libraries/headers. If you do not want to provide example, then let me keep my opinion unchanged. If you want to write example, keep in mind that DirectX objects are COM objects, so you need to call ->Release() to free them.
SigTerm
@SigTerm: What folly is this, artificially reducing the tools to the point where `goto` might become the lesser evil and challenging me to do better with such crippled tools? I might even still win, since `std[::tr1]::shared_ptr` can take a deleter, but I'm not going to take up such silly a challenge. Had I ever done any COM work, I'd surely have smart pointer-like wrappers in my toolbox that call `->Release()`. And I would not even _attempt_ to write a bug-free application without such. (Among other reasons because they work in the face of exceptions - which `goto cleanup` utterly fails to.)
sbi
@sbi: "What folly is this". Let me explain. My opinion is based on my experience - in certain situation goto has proven to be efficient for the task. But there may be a better way to do that, because I don't know everything. Your opinion may be based on your experience, or may be a some kind of "religious" belief ("goto is evil"). By asking for challenge with "crippled tools" I'm trying to clarify if you indeed have better knowledge and it is not a "religious belief". If you will demonstrate it, I may adapt and acknowledge it. You are free to refuse to accept "challenge". Your choice.
SigTerm
@sbi: "smart pointer-like wrappers" is there a warranty that smart poitners will be released in certain order when you aren't using nested scopes? "because they work in the face of exceptions" It is guaranteed that no exceptions will be thrown. "And I would not even attempt to write a bug-free application without such." This doesn't sound reasonable to me, because it is still possible (and isn't exactly difficult) to write bug-free application without them.
SigTerm
@sbi: "but I'm not going to take up such silly a challenge" As you wish. You made your choice, and you probably had your reasons. Have a nice day.
SigTerm
@SigTerm: _"is there a warranty that smart poitners will be released in certain order when you aren't using nested scopes?"_ There certainly is. C++ guarantees for all objects (except for dynamically allocated ones) that the order of destruction is the reverse order of their construction. That's one of the cornerstones of its object system and fundamental to RAII.
sbi
@SigTerm: I'm not sure what you're expecting of me. I have never done any COM, so I don't know its nooks and crannies, but, basically, what you do is this: `{ com_ptr<A> a=create_a(); com_ptr<B> b=make_b(a); com_ptr<C> c=make_c(b); if(c.is_empty()) return; f(b); g(a,b,c); }`. Presuming everything went right, at the closing `}`, first `c`, then `b`, then `a` will have their destructor called (which could then call `Release()` on the underlying COM object). If `f()` or `g()` throw an exception, the same happens. If making the `c` object fails and the function returns early, first `b` and...
sbi
...then `a` will have their destructors called. The same goes when `make_c()` throws. If `make_b()` throws, `a` will be destroyed. The beauty of this is that, except for the tests whether `c` is empty (and that would go away if `make_c()` either succeeded or threw), it shows just the real algorithm, and no error handling code - and yet it still works when errors occur. _I utterly fail to see how `goto` could make anything "more compact" here._
sbi
_"This doesn't sound reasonable to me, because it is still possible (and isn't exactly difficult) to write bug-free application without them."_ For many, many years, I had been part of a big project (several MLoC) which contributed to an applications that's quite likely to be installed on your machine, but also works as a stand-alone app on half a dozen platforms. Over the years, the number of crashing bugs in certain areas of the software correlated very nicely with whether developers used techniques like RAII (to which I also count using, e.g., `std::vector<>`) or managed resources manually.
sbi
For many years, my code threw about one _Access Violation_ per year - _during testing on my local machine_, before checkin. From Dec 2001 to May 2008 I wrote and maintained a piece of >100kLoC of that big project, sporting file IO, XML handling, a herd of different kinds of objects with n-to-m relationships between them, and other things that are fun to do (like optimizing loading of ~1000 such objects) across half a dozen platforms _plus_ a considerable part of the basic library underlying all of that company's software - and I did not check in a single crashing bug in all those years.
sbi
@sbi: "There certainly is. C++ guarantees for all objects (except for dynamically allocated ones) that the order of destruction is the reverse order of their construction." That's enough for me (you didn't really have to explain it in detail...). You win. Still, you should admit that in C (without ++) you would have to use goto for that. Have a nice day, your information was useful, the discussion is over.
SigTerm
@SigTerm: Yes, `goto` might make certain things easier in C. I wouldn't know, since I have very little real experience in C.
sbi
+2  A: 

1) No limitation really, except that you should follow programming guides so as to use them properly (i.e. avoid dinosaurs when using goto's).

2) Yes, its used, and Boost also (which is kinda the preview of the next STL).

3) Mix, always a mix. To say that C++ is only object-oriented is to misunderstand the language. "Everything is an object" is really a Java (or things like it) thing; check out C++'s concept of POD, as an example of how not everything is an object.

4) same as 1)

5) It is used, but as far as I've seen, no new project is being created in C.

6) No... he kinda said the opposite of what I've just said.

Gianni
Our answers reinforce eachother almost to the letter. Wow.
glowcoder
@glowcoder I guess we do have a point then. ;-) I'm not that surprised, while writing I tried to make a point about putting forward 'general consensus' more than my own opinions. I, for one, love goto's! But really, its mostly stuff we all learn after doing C++ for s while, and any smart programmer does.
Gianni
A: 

Generally, some parts of C++ are limited for patform and performance reasons.

Exceptions and RTTI are normally disabled. Exceptions are expensive when they unwind the stack, and RTTI uses too much memory. Lack of exceptions is particularly a pain because rather than come up with another error handling system, programmers tend to just return NULL. :)

Floats are used almost exclusively over doubles, for performance reasons.

Virtual functions and inheritence can be used, but the virtual table lookup can be costly so sometimes it's optimised out.

The STL can be used, but you normally have to write your own allocators to avoid fragging memory (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html). It's more common to see custom containers - of varying quality. I've never come across boost being used in a console project.

Most code is OO, almost to a fault.

tenpn
A: 
  1. Are C++ game developers usually limited on using all the C++ features? Is it true more or less for each platform? These days, I think it's pretty good on the main console platforms but if you were developing for mobile devices I doubt it's quite so even
  2. Is STL used these days in the environment, or should I avoid it? It is used and should be used... but some people don't trust it because in the past STL support was much less even, and sometimes had performance problems
  3. Do they treat C++ as "everything must be an object" or is it a mix of paradigms? Far too general. Varies from person to person and company to company. On average, game developers have a reputation for being less focused on software engineering and more on hacking code, but that's far too generalised to really be of use. However spouting for 30min about design patterns and frameworks and abstractions in an interview is probably not a wonderful plan
  4. Any features they tend to not use? STL still has a bad rep amongst those who worked on older platforms. Exceptions also and RTTI, probably sicne they weren't well supported (and maybe are still not)
  5. Is C still used, and is it good to show a project completed in it? I'm sure it is, and if you have a project then show it, but it's not anywhere near as widely used as C++

I did talk to a famous game designer and he said they don't use STL and said to use basic encapsulation with classes, but he said not to use all other advanced C++ features. The idea is to keep it simple. Is this common? That view is pretty common but it doesn't mean it's right. You might want to tread carefully to avoid upsetting a 20-year veteran who has not moved with the times

John
+5  A: 

Bear in mind that "games development" covers everything from phones to websites to PSPs and DS to Wii, Xbox, PS3 and PC/Mac, so the answers you get may vary from one product/developer/platform to another.

1) Yes. Games development can mean that you have to use a particular compiler for your target platform, which may not support some of the "newer" features of C++. You may be restricted on which libraries the development studio will allow you to use. In addition, hardware restrictions on some platforms may influence how you code (e.g. on some systems the cost of virtual functions can be excessively high compared to a PC, so your designs can be restricted away from pure OO for performance reasons. Some systems have limited resources (memory, disk, etc) which will influence your design a lot. On the Playstation3 you may need to write vector-processing SPU tasks rather than traditional single/multithreaded code. etc).

2) Yes. (it's commonly used, as there's usually no point wasting time rolling your own, probably less efficient, equivalents)

3) A mix. Generally in games the key goals are (in rough order): Deliver the impossible on time (why are you going home now, it's only 10pm?), deliver high performance, and try not to crash.

4,6) Generally keep the code simple and flexible so you can develop fast and change to suit the producer's latest vision. You usually have tight deadlines so a lot of design and careful coding tends to be difficult to achieve. The industry unfortunately tends to work on the "churn out the code quickly so we can throw it away and start on the next product", although it is slowly coming around to the idea of re-usable libraries (and more methodical professional practices like unit tests, etc).

A typical scenario is to be asked to code up a quick and dirty demo for the boss, and then when you think you'll be able to throw it away and write it properly, the boss says "we don't have time to rewrite something that already works, start on the next level". So if you're asked to be quick'n'dirty, try not to burn too many bridges or you'll regret it.

5) You may use C, but it depends on the platform and the development house. These days most devlopment will be C++ even if parts of the code are effectively "little more than C" in terms of syntax and features used.

Jason Williams
+1  A: 

Best C++ programming practices in professional game development environments?

The question is too broad.

Are C++ game developers usually limited on using all the C++ features? Is it true more or less for each platform?

No, or at least extremely unlikely. Depends on platform/compiler.

Is STL used these days in the environment, or should I avoid it?

You shouldn't avoid it, but:

  1. you'll definitely need a profiler if you want stl containers
  2. STL containers are extremely slow in debug builds. This might be a problem, because if you're frequently accessing them, there will be a huge performance drop (say, from 300 fps to 50) in debug build (when compared to release build). Debug code is already slower, and stl containers have large amount of "security check" in debug build.

Do they treat C++ as "everything must be an object"

Every idea is bad if you take it to extremes. "Everything must be an object" is one of those.

Any features they tend to not use?

Why? Unless there is a compiler bug or performance loss there is no point in avoiding feature. Besides, there are commercial 3D games written in java, so I see no point in avoiding feature of compiled language.

Is C still used, and is it good to show a project completed in it?

Some opensource projects may use C, but I can't remember a game completely written in it. There is Q3 engine, but afaik it is mix of C/C++.

I did talk to a famous game designer and he said they don't use STL and said to use basic encapsulation with classes, but he said not to use all other advanced C++ features.

They could decide to avoid stl simply because containers are slow in debug build. It is unclear what exactly you call "advanced C++ features". Also, keep in mind that he is designer, not programmer.

The idea is to keep it simple. Is this common?

The idea may or may not be common, but it is a correct idea for a small teams. With limited human resources everything should be made as simple as possible - to reduce development costs. If you stick to concept like "everything should be an object", decide to build "nice-looking" class hierarchy, etc, AND if you have limited resources, then you are doomed - It is very possible that you'll never finish your project and will be rewriting code forever and trying to decide what should be derived from what. Needless project flexibility and extensibility may kill small project. Game engine doesn't have to be complicated.

Large teams may afford complex solutions, but complexity leads to additional bugs. For example, you could look at sims 3 - it has a VERY advanced system (object "plugins", game resource management, GPU resource management, content "streaming", etc, and I think they even put game logic into a few .NET modules that are stored within game archive...), but extensibility of the system resulted in mountain of bugs after certain updates.

SigTerm
A: 

I'm going to assume you're referring to people who develop full-price games for consoles and PC.

"Are C++ game developers usually limited on using all the C++ features?"

I think most are avoiding exceptions, RTTI, and many avoid the STL part of the standard library. They're probably using templates, though not much metaprogramming. I don't think this varies much across platform. But it does vary from company to company.

"Is STL used these days in the environment, or should I avoid it?"

It is used by many, but not all. Don't avoid it - it's a useful tool that is very good at the tasks it is designed for. Just make sure you can get by without it.

"Do they treat C++ as "everything must be an object" or is it a mix of paradigms?"

Different places will work in different ways. There's nothing intrinsic to object orientation in C++ that makes it good or bad for game development. Bear in mind that there is often legacy code or 3rd party libraries that are written in C or designed to be operated from C, so it's very rare that you'll have C++ throughout.

"Any features they tend to not use?"

I think we covered this above.

"Is C still used, and is it good to show a project completed in it?"

Yes, and no. Life is too short to spend on mastering every related language and dialect before you get your first job. Showing the ability to use a C-style library (eg. SDL) should suffice. Obviously if you set your goals on joining a company that just uses C, you will have to consider that.

"I did talk to a famous game designer and he said they don't use STL and said to use basic encapsulation with classes, but he said not to use all other advanced C++ features. The idea is to keep it simple. Is this common?"

See above. Incidentally, game "designers" typically aren't programmers, so watch your terminology. If you apply for a game designer role, you won't usually be showing programming skills.

Kylotan
+1  A: 

Electronic Arts as one example uses STL, but felt compelled to create their own version of it called EASTL.

Mark Ransom