views:

188

answers:

4

Hello all,

I soon started a small C++ project and elaborated some naming convention rules for variables, according to scope.

I made class members _prefixed_with_underscore and methods parameters suffixed_with_underscore_. I soon got neurotic about inventing naming conventions for everything, like global variables (ok, those might be important), inline global functions, to try to improve code readability.

I've read this question and its answers that answered some of my doubts about naming convention, specially about methods parameters names. Maybe having a_rule_like_this_ for parameters names might not be a good idea.

So, my question is, what naming conventions do you use for different "entities" when programming, specially for parameter names? Thanks.

+1  A: 

Never make any type of identifier that uses a prefixed '_'. The prefixed underscore is reserved for compiler implementers to use for their own neferious purposes. Meaning its perfectly valid for C/C++ compiler implementors to create macros, implicit local variables, namespaces, as long as they have at least one underscore.

If your code conforms to the c++ standard in every other way, the presence of prefixed underscores on identifiers mean that there is no expectation that it will compile on other compilers, or different versions of the compiler you are using right now.

Chris Becke
The restrictions are in 17.4.3.1.2 [lib.global.names]. User code cannot define identifiers containing '__' or beginning with '_X' where 'X' is a capital letter anywhere. User code cannot define identifiers beginning with '_' in the global namespace. Naming attributes with names starting in '_x' with 'x' being a lower case letter is correct.
David Rodríguez - dribeas
@David: It's technically correct, but it's easy to assume that because some '_'-prefixed names are correct that all of them are correct. Better to avoid the problem altogether.
Bill
I stand corrected.
Chris Becke
+2  A: 

There are many people who have created conventions - google for c++ naming convention

Also there are style guides which also suggest ways of programming.

You will probably not agree with all the points in one but being consistent in your project/team/company is the important one.

Also naming conventions will differ for different languages (and across OS) So don't use PHP suggestions for C++. (the preceding _ gives a problem fo C++) For C++ the most common conventions are that parameters are normal text but instance variables either end in _ or start with m_ (I prefer the former)

Mark
A: 

Do not prefix identifiers with an underscore, as some compilers reserve them for their own non-standard purposes.

I like the naming conventions in the Google C++ style guide.

Ton van den Heuvel
Unfortunately I don't. I use them. But I don't like them. Trailing underscores look very untidy when coupled with other c++ markup. Prefixed underscores admittedly arn't much better.
Chris Becke
+1  A: 

This is little more than a matter of opinion. So, here's mine.

  1. Don't sweat the small stuff
  2. Check out other people's opinions on the matter (which you're doing)
  3. It matters less which convention you choose, and much much more that you apply it consistantly
  4. Here's some of my convention:

    class MyGizmo
    {
    public:
      int DoIt();
    private:
      string myString_;
    };
    
    typedef vector<MyGizmo> MyGizmos;
    
    namespace somewhere
    {
      MyGizmos gizmos;
    };
    
    
    int MyGizmo::DoIt()
    {
      int retVal = 0;
      string strCopy = myString;
      retVal = strCopy.length();
      return retVal;
    }
    
John Dibling