Re: getters and setters

Discussion in 'C++' started by Steven T. Hatton, Jun 21, 2005.

  1. Victor Bazarov wrote:

    > Steven T. Hatton wrote:
    >> kelvSYC wrote:
    >>>I'm familiar with get and set function paradigms from Java, but what's
    >>>the recommended design for such in C++? Should it be like so:
    >>>foo& getFoo();
    >>>void setFoo(foo& f);
    >>>or like so:
    >>>foo& foo();
    >>>const foo& foo() const;
    >>>or some combination of the above?

    Basically, you are talking about what some people call the JavaBeans naming
    JavaBeans Spec is a worthwhile read for any programmer, Java or better.

    >> My preference is:
    >> class C{
    >> int x() { return _x; }

    > int x() const { return _x; }


    >> void x(int x_) { _x = x_; }
    >> private:
    >> int _x;
    >> };
    >> But I believe I am not in the majority on this.

    > I can see why. If the function takes an argument, then the name of the
    > function should be a verb.

    Bah! If it were more complex, I would agree that an English (or other
    natural language) verb would be advisable, but in C++, if I follow my
    convention, "void x(int x_) const;" is a verb, "set" is /implied/.
    See page 54 javaCodingStandards.pdf

    Also here:

    I hold Matthias Ettrich in the highest regard. He is one of the most
    successful programmers in the world. To some extent, I believe his advice
    applies better to the kinds of APIs he works with, than to the kind of
    stuff I'm doing. Nonetheless, I am not fully sold on the setMember and
    maybe getMember, naming convention. There is merit in the suggestion to
    use whole words rather than abbreviations. That way, you don't have to try
    and remember both the concept and the shortcut. OTOH, Stroustrup makes a
    good point about the expressive power of consice symbols such as operators.
    Sometimes x, y, z, are more expressive than widthComponent,
    heightComponent, depthComponent.

    > C c;
    > c.x(5);
    > what does that do?

    If you see a variable-looking function name, and no verbage tacked onto it,
    It's a "set" function. I will agree that bool isSomeState(); and
    setSomeState(bool s); can be marginally more expressive than bool
    someState(); someState(bool s);

    > Now, if it were
    > c.x() = 5;
    > or
    > c.set_x(5);
    > I'd not argue.
    > V

    Germanic languages do not have genuine preposition.

    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
    Steven T. Hatton, Jun 21, 2005
    1. Advertisements

  2. Sammy

    Sammy Guest

    "Steven T. Hatton" <> wrote in

    > Bah! If it were more complex, I would agree that an English (or other
    > natural language) verb would be advisable, but in C++, if I follow my
    > convention, "void x(int x_) const;" is a verb, "set" is /implied/.

    'Implication' is often not totally clear. What the writer thinks he's
    implying may not be the same as what the reader thinks he is. This is
    especially true if the two dont read things the same way, or there is some
    language barrier.
    Sammy, Jun 21, 2005
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Armhold
    Chris Riesbeck
    Sep 2, 2004
  2. Getters and Setters

    , Jun 9, 2006, in forum: Java
    Eddy Young
    Jun 10, 2006
  3. Daniel Fetchinson

    setters and getters in python 2.6 and 3.0

    Daniel Fetchinson, Nov 29, 2007, in forum: Python
    Daniel Fetchinson
    Nov 29, 2007
  4. TechieInsights
    josh logan
    Feb 12, 2009
  5. Lachlan Hunt
    Lachlan Hunt
    Mar 28, 2005

Share This Page