Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing underscores for member variables in C++

I've seen people use a trailing underscore for member variables in classes, for instance in the renowned C++ FAQ Lite.

I think that it's purpose is not to mark variables as members, that's what "m_" is for. It's actual purpose is to make it possible to have an accessor method named like the field, like this:

class Foo {
public:
    bar the_bar() { return the_bar_; }
private:
    bar the_bar_;
}

Having accessors omit the "get_" part is common in the STL and boost, and I'm trying to develop a coding style as close to these as possible, but I can't really see them using the underscore trick. I wasn't able to find an accessor in STL or boost that would just return a private variable.

I have a few questions I'm hoping you will be able to answer:

  1. Where does this convention come from? Smalltalk? Objective-C? Microsoft? I'm wondering.
  2. Would I use the trailing underscore for all private members or just as a workaround in case I want to name a function like a variable?
  3. Can you point me to STL or boost code that demonstrates trailing underscores for member variables?
  4. Does anybody know what Stroustrup's views on the issue are?
  5. Can you point me to further discussion of the issue?
like image 970
eomer Avatar asked Sep 06 '10 10:09

eomer


People also ask

Can C variables have underscores?

A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore.

What is a trailing underscore?

Single trailing underscore naming convention is used to avoid conflicts with Python keywords. When the most fitting name for a variable is already taken by a keyword, appending a single underscore convention is followed to break the naming conflict. Typical example includes using class or other keywords as variables.

Can you use _ in variable names?

Variable names can never contain spaces. The underscore character ( _ ) can also appear in a name.

What is _ after variable in C++?

In C++, identifiers starting with an underscore, followed by a capital character. identifiers having two consecutive underscores anywhere. identifiers in the global namespace starting with an underscore.


3 Answers

In C++,

  1. identifiers starting with an underscore, followed by a capital character
  2. identifiers having two consecutive underscores anywhere
  3. identifiers in the global namespace starting with an underscore

are reserved to the implementation. (More about this can be found here.) Rather than trying to remember these rules, many simply do not use identifiers starting with an underscore. That's why the trailing underscore was invented.

However, C++ itself is old, and builds on 40 years of C (both of which never had a single company behind them), and has a standard library that has "grown" over several decades, rather than brought into being in a single act of creation. This makes for the existence of a lot of differing naming conventions. Trailing underscore for privates (or only for private data) is but one, many use other ones (not few among them arguing that, if you need underscores to tell private members from local variables, your code isn't clear enough).

As for getters/setters - they are an abomination, and a sure sign of "quasi classes", which I hate.

like image 113
sbi Avatar answered Oct 10 '22 09:10

sbi


I've read The C++ Programming Language and Stroustrup doesn't use any kind of convention for naming members. He never needs to; there is not a single simple accessor/mutator, he has a way of creating very fine object-oriented designs so there's no need to have a method of the same name. He uses structs with public members whenever he needs simple data structures. His methods always seem to be operations. I've also read somewhere that he disencourages the use of names that differ only by one character.

like image 16
forceal Avatar answered Oct 10 '22 11:10

forceal


I am personally a big fan of this guideline: http://geosoft.no/development/cppstyle.html

It includes omitting the m_ prefix, using an underscore suffix to indicate private member variables and dropping the horrid, annoying-to-type habit of using underscores instead of space, and other, more detailed and specific suggestions, such as naming bools appropriately(isDone instead of just done) and using getVariable() instead of just variable() to name a few.

like image 13
identity Avatar answered Oct 10 '22 09:10

identity