What the respected community prefers, and how their preferences argue:

Traditional option:

class TFoo { int value; public: int GetValue() { return value; } void SetValue(int v) { value = v; } }; 

More visual (?) Option:

 class TFoo { int value; public: int ValueGet() { return value; } void ValueSet(int v) { value = v; } }; 

Closed due to the fact that off-topic participants Kromster , Peter Olson , Vladimir Glinskikh , aleksandr barakin , Sergey Snegirev Sep 9 '15 at 9:59 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Questionnaires are forbidden on Stack Overflow in Russian . To get an answer, rephrase your question so that it can be given an unambiguously correct answer." - Kromster, Vladimir Glinskikh, aleksandr barakin, Sergey Snegirev
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • The meaning of such questions is to understand how to write extremely obvious and visual code that does not cause unnecessary questions (and disgust). - Spym
  • Everyone has their own preferences and he is unlikely to recede from them. Except when the naming rule is governed by corporate agreements. So similar questions - a pounding of water in a mortar. - IAZ
  • I understand that the point is not to force someone to write somehow, but to write it yourself so that others can read without convulsions :) - cy6erGn0m

2 answers 2

A more visual variant (IMHO) in the style of a verb denoting an action , then a noun, to which this action is directed . Those. getValue () and setValue ().

Try to “taste” two phrases: get value and value get . The first one sounds more natural.

  • The second sounds more logical: first the entity is declared, then the manipulations performed with it. In general, rules that are obvious to natural languages ​​are hardly applicable here. That's why I asked a question. - Spym
  • The program code reads all the same by a person, and (perhaps unconsciously) applies the usual rules of natural languages ​​to it. But again, this is my IMHO. I agree with IAZ on personal preferences, have not yet created a uniform code of rules for the design of a code for all languages ​​:) - Equinox
  • @Equinox, however, rules have been created for many languages. And it is right. - kirelagin
  • @kirelagin is more likely not for languages ​​(although there is one for some), but rather for libraries and technologies like COM in which properties are implemented by adding "get_" and "put_" for a variable. But whether it is right or not - time will tell, but for now everyone will use what they want :) - IAZ

Whenever possible, getters and setters should be avoided. Arguments:

  1. The setter and the getter are essentially the same as referring to members of a class, only the more ugly way of doing so.
  2. To construct an object with specific properties, use constructors with initialization lists. And an overload of constructors for argument types.
  3. Classes need to be designed so that their member functions perform meaningful actions. And to reduce them to the level of setters-getters, for sure, is not necessary.

Plus, there are only one getters and setters - with their total use, it is possible to log calls to variables. Those. debugging might be easier. But, given that it is still possible to directly access variables, it will be tempting to use one method or the other.

Regarding the question "what's better?"

It seems to me that the getValue / setValue option is better than any other (get_ / put_, ValueGet / ValueSet, etc.). Better yet, abandon get / set and call accessors by data name. For example:

 class my { private: int size_; ... public: int size(void) {return size_;} void size(int s) {size_ = s;}//хотя зачем? тут еще должна быть куча логики // так что лучше ввести ф-цию resize(int) ;) ... } 

PS: and the names of classes (types) are also besy with the letter T. Why? See here

PPS: I also found an interesting variant of naming in Qt :

  • The answer to 1 is that setters and getters are not "more ugly" ways of addressing class members, but protect class members from setting them to wrong / unintended / erroneous values ​​— setting the member as private, creating a getter and setter for validation the set value can be guaranteed to the program itself and the absence of run-time errors associated with the incorrect transmitted data. - gote 1:56 pm
  • Okay Make a Cartesian point class. With setter and getter. And I will show that it is incorrect. <i> the program has no run-time errors associated with incorrect transmitted data. </ i> <p> This is a provocation, because it is easy to setter to translate an object into an incorrect state if the correctness of the values ​​of a pair (or more) of variable class members has some kind of tricky dependence. The current does not need to be told that you will use a setter of the form struct _x {int a; int b}; setStructBlaBlaBla (struct _x x); - I will not believe it. <p> Besides, the getter should be made const and inline. - gecube
  • And if you need to do something in the spirit of the number of int, lying in a certain range, it is convenient to use the appropriate template. - gecube