Jump to content
  • Advertisement
  • entries
  • comments
  • views

On C++ Naming Conventions

Sign in to follow this  


I threw myself back into the deep end of C++ again a few months ago, having spent the last couple of years with an emphasis on C# and .NET.

One thing that I'm thinking of is the old subject of coding style. Every software house tends to have a house style, so at work you just adopt that - but at home, on personal projects I find myself drifting around stylistically, at least with naming conventions.

There's a few main styles that I tend to consider ok for me:

.NET Style
Microsoft have a standard set of style guides for .NET / C# programs which for the most part people adopt (although not always, even within Microsoft public code).

The .NET style is simple:

  • PascalCase for class names
  • PascalCase for public methods (all methods, in fact)
  • Interface classes start with an "I"
  • Public properties/variables are PascalCase
  • camelCase for variable names
  • Namespaces are PascalCase
  • Assembly Names are PascalCase

    Code would look something like this:namespace MyProject{ public interface IBar { } public class Foo : IBar { public void DoSomething(int parameterName); public int SomeProperty { get; set; } }}
    It's worth noting that the get/set property functions end up becoming something like "SomeProperty_get"/"SomeProperty_set" under the hood.

    Java Style

    Java programs also have a common style.

    • PascalCase for class names
    • Interface classes start with an "I" (but not always)
    • camellCase for public methods
    • Public properties/variables are camelCase, prefixed with getXX() or setXX() (as Java doesn't have properties)
    • camelCase for variable names
    • Namespaces are lowercase
    • Package Names are lowercase

      In Java, the above example looks something like:package myproject;public interface IBar { }public class Foo implements IBar{ public void doSomething(int parameterName); public int getSomeProperty(); public void setSomeProperty(int value);}
      C Style / C++ Standard Library Style

      C++ seems to have a tonne of styles. One of the first ones you'll come across is that of the standard library, which appears to have adopted the C style convention, largely due to remaining consistent with the old code from yesteryear.

      Here, we have:

      • lowercase_delimited for class names
      • Interface (abstract) classes aren't special
      • lowercase_delimited for public methods
      • Public properties/variables are lowercase_delimited, there doesn't seem to be a get/set style (?)
      • lowercase_delimitedfor variable names
      • Namespaces are lowercase

        Back to our example:namespace myproject{ class bar { public: virtual ~bar() { } }; class foo : public bar { public: void do_something(int parameter_name); int get_some_property(); void set_some_property(int value); };}
        Looking through some other sources and the Google C++ guide seem to lean to a blend of the Java Style with C++/Standard library style.


        • PascalCase for class names
        • Interface classes start with an "I" (but not always)
        • PamellCase for public methods
        • Public properties/variables are lowercase_delimited, prefixed with my_property() or set_my_property()
        • camelCase for variable names
        • Namespaces are lowercase_delimited

          This leads to:namespace my_project{ class Bar { public: virtual ~Bar() { } }; class Foo : public Bar { public: void DoSomething(int parameterName); int some_property(); void set_some_property(int value); };}
          With all this, the Google version seems to make a lot of sense.

          What's your style and how did you pick it?
Sign in to follow this  


Recommended Comments

I'd agree that using non-standard naming conventions for home projects is somehow incoherent, since you'll have to mix it up with coding standards of used libraries anyway. But I wouldn't call debating over that topic a waste of time in general, since it's a productivity related issue. On the other hand, there is a reason that modern languages do not use that lowercase-dashy style. For most of programmers I know camel/pascal case is way much easier to read and maintain, and for some of them it was one of the main reasons to use Java/C# and choose those as professional careers paths over C/C++.
I'd say: you choose your language, obey the rules. The unwritten ones aswell.

Share this comment

Link to comment

I follow a style similar to Google's style but slightly different probably due to my C and Java roots.

Namespaces are done in Pascal case.
Class Names are in Pascal case.
Interfaces (abstract) use the I + Pascal case.
Variables are all lowercase separated by _ but typically tend to be short and sweet names.
Functions/Methods are all lowercase separated by _ and again short and sweet.

so you tend to get something like this...


In C++ I typically would not use properties unless the variable is read only or if I need to do some background checking on the data first coming in.  Otherwise I would just expose the variable directly as being public.


namespace MyProject
    class Bar
        virtual ~Bar() {}

    class Foo: public Bar
        void action(int param);
        int data();
        void set_data(int val);

Share this comment

Link to comment
In my experience, there are very few cases in which interfaces are prefixed with "I" in Java, so I don't think it is the norm there. At least I've seen no such thing in the standard library (nor any other library I've used).
Months ago I started to prefix interfaces with "I" because I thought they were different enough from concrete classes and abstract classes to merit it. After a while I started to think that the whole purpose of interfaces is to keep the user from worrying about the internals. So my conclusion was that prefixing interfaces with "I" is counterproductive to their purpose.
The dedicated prefix singles them out as interfaces and prevents you from intuitively think of it as any other object. Probably there is a reason to single out interfaces when you're coding them or the supporting code around them, but for the user, their usage and "looks" should be indistinguishable from regular classes, or at least that's what I think.
I rely on what I consider less intrusive for distinguishing classes, abstract classes and interfaces by using different colors for them.
EDIT: I see lots of cane shaking here :D

Share this comment

Link to comment

Personally I don't like the "I" (hungarian notation?) notation, beside my own style rules, I have a big rule:


If something is occurring too often in class names, maybe is time for a new namespace (and that's how is born my Mesh namespace xD. of course there will never be a "I" namespace). That's it, I usually program by interfaces so almost everything in public API should be prefixed with I wich is not good, even in that case I still prefer a " 'Interfaces' namespace" rather than a big "I" in front of the name. Interface is about "how to use" an object, so it doesn't matter if it is pure virtual (IMHO).


Underscored names are in google style guide, but I think the real reason for them is that "_" names are easier to read for search engines (should we optimize our code also for search engines? O.o). I prefer PascalCase for classes, namespaces and functions. and camelCase for methods and member variables (widely adopted convention)


My style is not truly mine, in the mean that I found few girls in my classroom wich was writing really good-lookin code and I tried to figure out how to write it myself (newlines and indentation). I also asked permission for copying that style and immediatly I had a better code :D. Don't invent a style, just find one that you like and use it :D

Share this comment

Link to comment

One thing that is interesting is that sometimes at the start of a coding project it's not quite clear if you want a pure interface (eg: no data, no method implementations) or an abstract class, which contains some common cruft.


Obviously, you can start with an interface and then base an abstract class on it to be your base class for more specialized implementations, but that does seem to start getting a little nasty.


This case supports the comments about interfaces not starting with "I", so that's an interesting point you guys are making.

Share this comment

Link to comment

Whether it's an intent to write better code or just a severe case of obsessive compulsive disorder, i think programmers have made a big case of naming convertions just because we've been 'programmed' to always seek for the -best- possible solution. But terrifyningly, i think this subjects falls into one of those dreaded "it's-all-very-subjective" cases.


The way I adapted a naming convension was progressively looking at other people's source code, and one convension rule that they've used i'd like, and another one i'd hate. At one point, I looked at some big guys' source code for conventions myself, but I just didn't like all of em, some felt weird to me.


So through time, I'm now writing code like this:


  • PascalCase for class names
  • camellCase for all methods, properties, variable names
  • Namespaces, Package Names are lowercase
  • Interface classes start with an "I"
  • Abstract classes start with an "A"
  • Enums start with an "E"
  • constant names are all CAPS.
  • private methods, properties, variables, are prefixed with "__"
  • protected methods, properties, variables, are prefixed with "_"
  • function arguments are prefixed with "p_"
  • local variables are prefixed with "l_"
  • brackets take a line on their own and everything inside is indented forward one step


Random copy paste from some source code:

 * Main Display Service Class
 * @author Aris Kostakos
class Display extends AService implements IDisplay
	//@todo: Reflection needed here!!! See above todo
	//@think: this is a nice way to check if a new view/scene/entity/etc has been created.

	public var space( default, null ):IGameEntity;
	protected var _invalidated:Bool;
	public function setSpace(p_gameEntity:IGameEntity):IGameEntity
		//cast the gameEntity, and complain if space already set.
		if (space != null)
			//@note if another space already exists, maybe you can warn better the user, or take
				//other action. may need rerendering, changing renderers, etc, etc, etc
			Console.warn("A space object is already bound to the Display service! Rebounding...");

		space = p_gameEntity;

		return (space);

It works for me so I'm happy...

Share this comment

Link to comment

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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!