ApplePieIsGood
ApplePieIsGood

Reputation: 3731

What is the preferred naming conventions du jour for c++?

I am quite confused by looking at the boost library, and stl, and then looking at people's examples. It seems that capitalized type names are interspersed with all lowercase, separated by underscores.

What exactly is the way things should be done these days? I know the .NET world has their own set of conventions, but it appears to be completely different than the C++ sphere.

Upvotes: 6

Views: 2242

Answers (5)

H. Rittich
H. Rittich

Reputation: 845

Historically, people were writing C++ before the STL was created. Hence, when the STL was created, the coding style of it was one among many. Until this day there is no naming convention that is universally accepted. Since we do not have universal consistency, the best you can aim for is consistency within your project/company/team.

It is, however, a good idea to use some existing naming convention instead of making up your own. Below is a list, sorted alphabetically, of popular naming conventions that are in use today.

Upvotes: 0

Rob
Rob

Reputation: 78628

Find something you feel comfortable with and stick with it. Some form of style is better than no style at all and don't get too hung up on how other libraries do it.

FWIW I use the Google C++ style guide (with some tweaks).

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Upvotes: 0

Nick
Nick

Reputation: 6846

It's going to be different depending on the library and organization.

For the developer utility library I'm building, for example, I'm including friendly wrapper modules for various conventions in the style of the convention. So, for example, the MFC wrapper module uses the 'm_typeMemberVariable' notation for members, whereas the STL wrapper module uses 'member_variable'. I'm trying to build it so that whatever front-end is used will have the style typical for that type of front-end.

The problem with having a universal style is that everyone would have to agree, and (for example) for every person who detests Hungarian notation, there's someone else who thinks not using Hungarian notation detracts from the basic value of comprehensibility of the code. So it's unlikely there will be a universal standard for C++ any time soon.

Upvotes: 0

Drew Dormann
Drew Dormann

Reputation: 63765

What a can of worms you've opened.

The C++ standard library uses underscore_notation for everything, because that's what the C standard library uses.

So if you want your code to look consistent across the board (and actually aren't using external libraries), that is the only way to go.

You'll see boost use the same notation because often their libraries get considered for future standards.

Beyond that, there are many conventions, usually using different notations to designate different types of symbols. It is common to use CamelCase for custom types, such as classes and typedefs and mixedCase for variables, specifically to differentiate those two, but that is certainly not a universal standard.

There's also Hungarian Notation, which further differentiates specific variable types, although just mentioning that phrase can incite hostility from some coders.

The best answer, as a good C++ programmer, is to adopt whatever convention is being used in the code you're immersed in.

Upvotes: 9

tfinniga
tfinniga

Reputation: 6849

There is no good answer. If you're integrating with an existing codebase, it makes sense to match their style. If you're creating a new codebase, you might want to establish simple guidelines.

Google has some.

Upvotes: 2

Related Questions