Sign in to follow this  
EvilNando

C++ coding standard

Recommended Posts

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 [url="http://hobbit-hole.org/cpp_coding_guidelines.html"]C++ Coding Guidelines by David Abrahams and Nathan Myers[/url] (an old document which pretty much is the Boost naming conventions) and [url="http://msdn.microsoft.com/en-us/library/ms229002.aspx"].NET Guidelines for Names[/url].

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

[b]Boost[/b]

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.

[b].NET[/b]

.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.

[b]Edit[/b]

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.

Share this post


Link to post
Share on other sites
EvilNando    96
[quote name='rip-off' timestamp='1305398235' post='4810797']
I'm not sure how a coding standard could be oriented towards game making. What do you think a coding standard is?
[/quote]


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

Share this post


Link to post
Share on other sites
GregMichael    135
" 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.

Share this post


Link to post
Share on other sites
SimonForsman    7642
[quote name='EvilNando' timestamp='1305477842' post='4811127']
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
[/quote]

Wow, just ... wow.

Share this post


Link to post
Share on other sites
rip-off    10976
Ok, here is a sub-convention. It may be added to any other coding convention to make a "game oriented" convention:
[quote]
[u][b]Game Orientation[/b][/u]
[list]
[*]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.
[/list]
[/quote]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this