C++ coding standard

Started by
6 comments, last by rip-off 12 years, 11 months ago
Please recommend me a C++ coding standard preferably one oriented to game making

thanks

Advertisement
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
I like to compare naming conventions so wrote a review of two good ones below... I only consider naming here, not any other conventions.

I have tried the naming conventions from C++ Coding Guidelines by David Abrahams and Nathan Myers (an old document which pretty much is the Boost naming conventions) and .NET Guidelines for Names.

Both conventions has chosen to name pretty much everything in the same way.

Boost

Boost names identifiers using lower_case_and_underscores except for template parameters which are PascalCase. I believe the reason is to follow the C++ Standard Library conventions but also because in C++ the distinction between different kinds of identifiers can be blurry. For example std::sort can take either a function or an object as the comparator or an enum value can be used as a result variable in template meta programming.

Following the Boost style makes the code blend in with the standard libararies and Boost very nicely but I found that i got a lot of name collisions between types and variables. One can argue that you should come up with better names such as "player* shooter" but sometimes it seems impossible so I ended up typing "player* plr" and stuff. In the long run it was too much so I started using Pascal_Case_With_Underscores for type names and this worked fine.

After a while I got tired of this style and somehow the code doesn't look very firendly so now I try the .NET naming conventions instead.

.NET

.NET uses PascalCase for all visible identifiers and I think the point is to get naming collisions so it is easier to write code that is compatible with case insensitive languages. The nice thing here is that it only applies to names that are in any way public (or protected). Function parameters are named with camelCase and it is common to use camelCase for local variables and private data members too. Private variables are often prefixed with _.

This gets rid of most of of the name collisions I had using the Boost style. When I do get a collision it is because a struct has a public member that is named the same as it's type but you just have to come up with better names in those cases. Instead you can name variables the same as functions such as "Player* nearestPlayer = NearestPlayer()" or "Vector length = Length(vector)".

So far I like the .NET conventions. I think that PascalCase and camelCase styles are more common.

Edit

I have found that name collisions with C++ standard naming conventions is not a big problem. To declare a member variable with the same name as a type you just need to repeat the namespace before the type and it will work. Local variables with the same name as a type works without any tricks most of the time. Even when a type name is hidden by a variable name operator:: can be used to access static members of a class without repeating the namespace.
I'm not sure how a coding standard could be oriented towards game making. What do you think a coding standard is?

I'm not sure how a coding standard could be oriented towards game making. What do you think a coding standard is?



My last 2 jobs had coding standards adapted to their field of work I'm too lazy to give you any more explanations so use google
" I'm too lazy to give you any more explanations"

So why should rip-off or anyone else try and help you ?

From my experience, different companies have different coding standards...there isn't one book or reference that will tell you which one to use...they will all be different.

Try Google.

My last 2 jobs had coding standards adapted to their field of work I'm too lazy to give you any more explanations so use google


Wow, just ... wow.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Ok, here is a sub-convention. It may be added to any other coding convention to make a "game oriented" convention:

Game Orientation

  • All types, functions and data should be in some way useful to the making of games. Types, functions or data that have no use in game making (direct or indirect) are discouraged.

[/quote]

This topic is closed to new replies.

Advertisement