tags:

views:

736

answers:

8

Allright, doing some project with few friends, and I need some standard for naming things in c++. Does anyone have any good naming scheme for c++ that is well thought-out and not made in like 10min.

Example, int* house should be named int* house_p, so that when someone reads the code, he doesn't need to scroll all the time wondering if a thing is a pointer, array, matrix, or whatever...

Post your well thought-out naming schemes that you are using !

+16  A: 

Example, int* house should be named int* house_p, so that when someone reads the code, he doesn't need to scroll all the time wondering if a thing is a pointer, array, matrix, or whatever...

But what if its type changes - are you going to go through all your code and change the names of all the variables. And what if the variable is an instance of a complex type:

ComplicatedDerivativeFinancialInstrument x;

What suffix will you use?

What you are asking about is known as Hungarian notation - its use in C++ is almost universally considered to be A Bad Idea.

anon
Yeah, please don't include type information in the variable name. It doesn't make sense. What should the suffix for smart pointers be? Their entire purpose is to behave as pointers, but they *aren't*. Especially when you start using templates, the precise type often doesn't matter.
jalf
Specifically, this is systems Hungrian notation, as opposed to apps Hungrian notation. (See also http://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation).
Steve Melnikoff
+1  A: 

There are good naming conventions out there, but included type information in the name of the variable(one form is called hungarian notation) is something that is becoming less popular. Most modern IDEs will give you useful information about a variable by just mousing over the variable, such as it's type. However, if it is important to you, hungarian notation is probably what you want.

AaronLS
The hungarian notation is one of the worst things one can use... It leads to discrepencies between types and naming during refactoring and is a complete nightmare. Even MS now discourages its use: http://msdn.microsoft.com/en-us/ms229045.aspx
jdehaan
Why the downvote? I noted that hungarian notation is not popular, but his question specifically asked for a naming convention that includes type information: "int* house should be named int* house_p, so that when someone reads the code, he doesn't need to scroll all the time wondering if a thing is a pointer, array, matrix, or whatever...". I gave him what he asked for. He didn't ask if it was good or bad, and the disadvantages are listed in the article I linked.
AaronLS
+4  A: 

In C++, when designing/refactoring code, it's quite common to change from a pointer (int*) to a reference (int&). If you had repeated yourself by including a suffix on the variable name indicating that it's a pointer, you would then have to change that to something else, all over the place. That seems like so much pointless make-work, that just makes the code harder to edit and shape into what you want.

You shouldn't need that kind of reminder; it will be quite clear from usage if a variable is a pointer or not, for instance.

unwind
+1  A: 

As Neil and aaronis have mentioned, you're talking about a form of Hungarian Notation. Since C++ is a strongly-typed language, it's generally considered a bad idea. It makes it harder to read the code, because there are a bunch of extra characters getting in the way.

The original Hungarian notation was actually used to signal information beyond what types told. For example, let's say I am doing some graphics work and I have points in various coordinate system. They're all of type Point, but some are in object coordinates, some are in camera coordinates, and some are in world coordinates. In that case, it makes good sense to name their respective variables something like oPt, cPt, and wPt.

Historically, this "apps" Hungarian seemed like a good idea (extra character codes to indicate non-type information) to some people and they went way too far turning it into "systems" Hungarian (extra character codes to indicate the C++ type), which is frowned upon these days.

Mr Fooz
A: 

As a general rule, use language of the real-world problem you're trying to solve. Your code will document itself when you use good, descriptive identifiers that make sense to the outside world.

As for syntax, choose a convention that's readable, conveys some information about the identifier, but doesn't lock you into some implementation. Make it simple and easy to remember, so your coders can spend more time coding and less time adhering to complex syntactical requirements. I find the following pretty easy on the eyes and informative enough:

  • Class and Method definitions - First letter capitalized with first letter of concatenated words also capitalized
  • Object instances and function arguments - first letter lower-case with first letter of concatenated words capitalized
  • Class data fields - trailing underscore, first alpha character lower-case with first letter of concatenated words capitalized

I'm afraid it probably doesn't pass the 10-minute test, but if it takes longer than 10 minutes to explain, it's probably getting in the way of real work.

class FooProblem {
  int fooData_;
  public:
  FooProblem(int fooData): fooData_(fooData){}
};
veefu
A leading underscore is reserved by the language. You can't do that.
John Dibling
Hmm... I know double-underscore is used for extensions, not aware of single-underscores being reserved. Do you have a reference you can point me to?
veefu
The leading underscore is reserved, but only at global scope - it is OK to use it as a name within a class.
anon
17.4.3.1.2: "Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace."
John Dibling
also along those lines: "Each name that (...) begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
John Dibling
I guess in re-reading these sections your code above is OK. Seems a little close to the line however, and I would prefer to name member variables with a single trailing underscore, rather than leading.
John Dibling
yeah, trailing underscore sounds better. I'll edit. Thanks for the tip.
veefu
My mistake, I should have said "namespace" not "scope".
anon
A: 

If there was a "I Hate Hungarian Notation" group on Facebook, I'd join it.

Here's some psudocode that illustrates my standard.

namespace utilities
{

class MyGizmo
{
public :
   void doTheThing();
   std::string name_;
private:
   int thingCount_;
};

static const char* ApplicationName = "Gizmotronic Ultra 1.0";

void setGizmoName(MyGizmo& gizmo, const std::string & name)
{
   gizmo.name_ = name;
}

int main()
{
   cout << ApplicationName;
   MyGizmo gizmo;
   gizmo.doTheThing();

   std::string gizmoName = gizmo.name_;

   return 0;
}
John Dibling