C++ naming: read_input() vs. readInput()

Viewed 43712

Which naming convention is more preferable in C++? The underscore method or the camelCase method? I have coded in Java for a while and I am used to the camelCase naming conventions. Which one is more prevalent?

Also, while defining a class, is there any preferred ordering of private/public/protected variables/methods?
Are friends usually put in the end?
What about typedefs, do they come at the top of the class definition?

10 Answers

Decades of coding as a job gave my fingers some disease. Whenever I use my little finger to press the [SHIFT] key for capital letter, I feel mild pain.

So I came to prefer snake_notation. Using AutoHotKey utility, I assigned [alt]+[space] combination to 'underscore character' of snake. It is little uncomfortable for my thumb to press [alt], but better than using little finger. (Actually [ctrl]+[space] is much better, but VisualStudio uses this combination as intellisense.) Also, I feel it is faster than camelCase.

I desire i-rocks launches new keyboard with 'underscore key' for programmers who prefer snake_notation.

For my own projects I have adopted the Apple Coding Conventions. During my time writing a decent amount of Objective-C code I came to love the self documenting nature of well written Objective-C code and the Cocoa library.

So in C++ I have stuck with the spirit of these conventions because in my opinion they improve readability and on several occasions getting the name right also helped me think through what the code needs to do.

SCREAMING_SNAKE_CASE:

FRAMES_PER_SECOND

PascalCase:

ClassName

camelCase:

descriptiveMethodName()

positionX

Underscore:

_isGameOver

Private/Protected/Public:

private:

protected:

public:

Apple Convention Examples:

  • Clarity and brevity are both important, but clarity should never be sacrificed for brevity.
bgColor(r, g, b, a)

A little more typing to do but the increase in clarity is usually worth it.

backgroundColorWithAlpha(red, green, blue, alpha)
  • Avoid names that are ambiguous.

There are too many examples to mention of this, but consider the introductory code examples from nearly any tutorial. Meaningful names increase clarity and reduce programmer error.

i vs index
x vs meaningfulName
posX vs positionX

I understand there are historical reasons for names like i, x, j, and hWPTR but in my opinion there is no reason to do them and they are awful for teaching.

Microsoft code examples (copied and repeated forever) are some of the worst offenders.

szStr
oObj

Seriously?!

Of course when collaborating with a team I am happy to adopt whatever the standard is. I want my code to look like it belongs to the project. The one thing that does make me twitch a little are weird braces.

K&R braces are the only correct braces. :)

bool hasComponents() {
    ....
}

If some languages come with naming conventions (e.g., https://peps.python.org/pep-0008/ for python), this is not the case in C++.

This might be linked with B. Stroustrup (the creator of C++) "disliking monoculture" (https://youtu.be/ZQds2aGHwDA?t=169).

Even the standards of c++ follows different naming convention (Thriving in a Crowded and Changing World: C++ 2006–2020, part 6.5 Naming of concepts). I don't think there is a prevalent style in the C++ community; I have not found statistics but different companies use very different styles.

As suggested by Freek de Bruijn, the key is consistency. If the project you are working on follows a convention, stick to it. If not, follow one of the existing convention suggested in Freek's post/ it would be easier than inventing your own.

Related