views:

373

answers:

7

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?
+2  A: 

Only speaking for myself... I always use trailing underscore for private data members, regardless if they have accessor functions or not. I don't use m_ mainly because it gets in the way when I mentally spell the variable's name.

usta
+2  A: 

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.

identity
Well most of this stuff is just personal aesthetic preference, up to the point that [this](http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586) great Coding Standard subsumes all of this as: 0. Don't sweat the small stuff. (Or: know what not to standardize.) ... Just be consistent.
Fabio Fracassi
I also prefer not to use `[get|compute]Data()`, because code like `use( texture.data() );` reads much nicer IMVHO, and doesn't distract the class user with details he shouldn't care about like whether the data is cached or needs to be computed.
Fabio Fracassi
I think `is_done` reads much better than `isDone`. `_` is much more like a space than no space at all.
GMan
+2  A: 

As far as I remember, it's not Microsoft that pushed the trailing underscore code style for members.

I have read that Stroustrup is pro the trailing underscore.

I remember because I completely disagree on this point.

There is one reason.

When I look at the code rapidly, I tend not to see the last characters, and I have often been mislead by trailing underscore, thinking I was dealing with a local variable. It happened more than once, so I have banished this style from my coding.

I mostly prefer the int m_iUsefulName, unsigned int m_uiUsefulName, std::string m_strUsefulName, bool m_bUsefulName style. It gives me all info at once. Type, scope, meaning, and I will never mistake with local variables.

Stephane Rolland
Do you remember where you read that? I'd be pretty interested because it is always interesting to read Stroustrup's reasoning.
eomer
ok let me sometime to remember ;-), I am almost sure it's on his website, but I should check.
Stephane Rolland
"It gives me all info at once" - until someone changes the type and forgets to update the pseudoHungarian tags. Then they stop being useful and become actively harmful.
Mike Seymour
http://en.wikipedia.org/wiki/Hungarian_notation: quotation from Bjarne: "No I don't recommend 'Hungarian'. I regard 'Hungarian' (embedding an abbreviated version of a type in a variable name) a technique that can be useful in untyped languages, but is completely unsuitable for a language that supports generic programming and object-oriented programming—both of which emphasize selection of operations based on the type an arguments (known to the language or to the run-time support). In this case, 'building the type of an object into names' simply complicates and minimizes abstraction"
Stephane Rolland
The big problem with Hungarian Notation is where you have to change the type, you're forced to update your source wherever that variable resides while this is easy in most editors, it can detract from the reviewer's attention when looking at diffs.
Johnsyweb
@Stephane: That quote is not really about underscores, is it?
eomer
@Mike maybe. As for me it means two things: If the modifier did not update the variable name: 1/ He is lazy and doesnt bother writing code that is understandable. 2/ There are too much occurence of the variable everywhere and this implied the variable is public and not accessed through an accessor.
Stephane Rolland
@eomer, no the Hungarian notation it the way for writing `IUnknown * m_pUnknown;` and Bjarne is against ( but you are right he doesnt say he is for the for the trailing underscore, though I thought there was only those two main trends for coding style )
Stephane Rolland
@Stephane There is also the possibility of not using any special naming style, just "foo" instead of "foo_" or "m_foo" or whatever. That's what Bjarne does in "The C++ Programming Language".
eomer
@eomer, there I have found this paper, about the GNU C++ style, and he is for the trailing underscore. http://www.scribd.com/doc/3020505/C-Programming-Style-Guidelines I don't know him, Fred Richards, and haven't read the entire text so I don't know what it's worth.
Stephane Rolland
+4  A: 

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 (e.g. compiler and standard library). 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 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.

sbi
I'm with you there, I avoid getters/setters wherever possible. But sometimes, they are actually useful - especially when using GOF design patterns. A simple example: Consider a class that owns and initialises objects (like the graphics subsystem in a game engine) and allows other classes access them. I'd use an accessor method, because declaring the variable public would allow read/write access, while I only want to allow read access. I can also return a const reference to ensure const correctness when using an accessor.
eomer
+2  A: 

I'm guessing that utopia would have been to use a leading underscore - this is quite common in Java and C# for members.

However, for C, leading underscores aren't a good idea, so hence I guess the recommendation by the C++ FAQ Lite to go trailing underscore:

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.


(ISO C99 specification, section 7.1.3)

nonnb
+1  A: 

As a maintenance developer that likes searchability I'm leaning towards m_ as its more searchable. When you, as me, are maintaining big projects with large classes (don't ask) you sometimes wonder: "Hmmm, who mutates state?". A quick search for m_ can give a hint.

I've also been known to use l_ to indicate local variables but the current project doesn't use that so I'm "clean" these days.

I'm no fan of hungarian notation. C++ has a strong type system, I use that instead.

FuleSnabel
+2  A: 

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.

forceal