Home > C++ > C++ Naming Conventions

C++ Naming Conventions

For grins, I perused a few books written by several C++ mentors that I respect and admire. I was interested in the naming conventions that they personally use when writing code. Here are the results.

Scott Meyers (Effective C++):

class names – class PhoneBook {};

member function names – void addPhoneNumber(const std::string& pn);

member attribute names – std::string theAddress;

Note: no annotation to distinguish class member attributes from local function variables.

Bjarne Stroustrup (The C++ Programming Language):

I consider the CamelCodingStyle of identifiers “pug ugly” and strongly prefer underscore_style as cleaner and inherently more readable, and many people agree. On the other hand, many reasonable people disagree.

class names – class Phone_book {};

member function names – void add_phone_number(const std::string& pn);

member attribute names – std::string the_address;

Note: no annotation to distinguish class member attributes from local function variables.

Herb Sutter and Andrei Alexandrescu (C++ Coding Standards):

class names – class PhoneBook {};

member function names – void AddPhoneNumber(const std::string& pn);

member attribute names – std::string theAddress_;

Note: they use post-underscores to distinguish class member attributes from local function variables (int clientName_;  //is a class member)

When I’m not constrained by a specific naming standard, I use this one:

class names – class PhoneBook {};

member function names – void add_phone_number(const std::string& pn);

member attribute names – std::string the_address_;

Note: Like Herb & Andrei, I use post-underscores to distinguish class member attributes from local function variables (int client_name_; //is a class member).

  1. RajKumar
    May 9, 2010 at 1:49 pm

    Just curious, which one of the style you use, I like to use the naming convention mentioned in Google style guide http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Naming . Have a look.

    • May 9, 2010 at 4:11 pm

      Hi Raj,

      I appended the post to reveal what I use. Thanks for the Google link.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: