10

Is it wrong to use m_varname as public and the same class with _variable as private

yesraaj
  • 46,370
  • 69
  • 194
  • 251
  • I'm used to this coding style and I find it very comfortable and clear: http://www.trinitycore.org/f/topic/6-trinitycore-developing-standards/ – LihO Dec 14 '12 at 12:47
  • @LihO the link says "*you don't have permission to view this content*". – Matthieu Mar 26 '19 at 12:52

6 Answers6

10

Some concerns:

  • Why do you have public variables?

  • Identifiers starting with _ and __ are reserved for system libraries. In practice this doesn't matter very often, but it's nice to be aware.

With those things said, there's nothing wrong with creating a naming convention, regardless of how it looks. Just be consistent.

Dan Olson
  • 22,849
  • 4
  • 42
  • 56
  • 2
    +1 for the consistency tip. You could have the worst naming convention ever, but if it's consistent, it will improve code maintainability. – Spidey Jun 22 '12 at 13:08
9

The same goes for C++ and for Java: you do not need any hungarian notation nor any prefixes/suffixes. You got keyword "this"!

class MyClass {
    private:
        int value;

    public:
        MyClass(int value) {
            this->value = value;
        }
}

Of course in this simple example you can (should!) use constructor initialization list ;)

So, instead using any awkward notations just employ language's possibilities. When you know the name of your member variable - you know that it is perfect. Why would you obfuscate it with "_"?

As for using the same names for public and private members: this absolutely wrong thinking! Why would one need two things to represent the same in the same class? Make it private, name it perfectly and give getters and setters public.

Marcin Gil
  • 68,043
  • 8
  • 59
  • 60
2

You should not use names that begin with an underscore or contain a double underscore. Those names are reserved for the compiler and implementation. Besides that restriction, you can use any naming convention you and your team likes. Personally, I hate any form of "Hungarian" notation and dislike the m_something notation as well. It really bothers me that if I need to change the type of a variable I need to go update its name everywhere where it occurs. That's a maintenance headache.

Brian Neal
  • 31,821
  • 7
  • 55
  • 59
1

Everyone has his/her own preferences as far as naming conventions are concerned. I'd say more people would agree on not having any public variables in a class.

Benoît
  • 16,798
  • 8
  • 46
  • 66
0

There are many C++ conventions out there. The key is to find one and/or adapt one. Stick with it, and be consistent. If you are working somewhere, try to get down as many of the conventions laid out. There's so many ones out there, and each one has good arguments, but they can contradict each-other (Joint Strike Fighter, Bell Labs, Mozilla, and so forth)

If there are different conventions between different parts of the project, at least make each file consistent within itself, and the .cpp and .h files should be consistent with each-other.

I find it better to be able to understand code written by different conventions so that you can easily adapt to a new working environment faster

Joe Plante
  • 6,308
  • 2
  • 30
  • 23
0

Assuming that you're working with C++, the my answer is NO. It's perfectly reasonable, but you'll should really stick to that convention.

However, statically typed languages such as C# assume such naming conventions to be somewhat redundant.

Personally I think it's ugly, but it's not apparent where a variable comes from in C++ as such the sugaring might help.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152