getters and setters style

前端 未结 13 2350
猫巷女王i
猫巷女王i 2021-01-01 12:05

(Leaving aside the question of should you have them at all.)

I have always preferred to just use function overloading to give you the same name for both getter and s

相关标签:
13条回答
  • There are several levels to "Getting" and "Setting"

    • I use Get and Set for "fast" operations.
    • If something will take a longer time to execute, then it will often be a Calc, as these names imply that some work has to be done to retrieve the result.
    • For longer operations, you start to get into prefixes like Load/Save, Query/Store, Read/Write, Search/Find etc.

    So Get/Set can be ascribed a useful meaning, and be part of a larger, consistent naming strategy.

    0 讨论(0)
  • 2021-01-01 12:12

    I'll go ahead and mention this should be a community wiki question.

    When I started learning C++ I looked for style guides, and Google's was good for some points:

    • Methods in uppercase (it's just prettier).
    • getters plainly and lowecase (rate).
    • setters explicitly and lowercase (setRate).
    0 讨论(0)
  • 2021-01-01 12:13

    Personally, I think getters and setters found in pairs are a code smell carried over from "visual" languages and their "properties". In a "good" class, the data members are writeonly or readonly but not read/write.

    I think the most common cause of getters and setters is not carrying the object model deep enough. In your example, why is total being passed the period and the rate? Aren't they members of the class? So you should only be setting the period and the rate and you should only be getting a total.

    There are probably exceptions but I just hate looking at a class and finding "getX/setX, getY/setY, etc. etc." It just seems there wasn't enough thought put into how the class SHOULD be used and rather the author made the class EASY to get at the data so he wouldn't have to consider how the class should be used.

    Naturally I am correct.

    0 讨论(0)
  • 2021-01-01 12:13

    I prefer to avoid the get and set labels, the information is not needed for the compiler to do it's job for most of these simple properties.

    you can have issues:

    class Stuff {
      void widget( int something ); // 'special' setter
      const Widget& widget( int somethingelse ) const; // getter
    }
    Stuff a; 
    a.widget(1); // compiler won't know which widget you mean, not enough info
    
    0 讨论(0)
  • 2021-01-01 12:14

    A few years ago, I would have agreed completely. More recently, a doubt began to make its way, because that makes taking the address of a getter or setter ambiguous. With tools like tr1::bind, this is really annoying.

    For example:

    struct A
    {
       void Foo(int);
       int Foo()const;
    };
    
    std::vector<A> v = ....;
    std::vector<int> foos;
    // Extract Foo
    std::transform(
       v.begin(), v.end(), 
       std::back_inserter(foos), 
       //Ambiguous
       // std::tr1::bind(&A::Foo)
       //must write this instead. Yuck!
       std::tr1::bind(static_cast<int(Foo::*)()>(&A::Foo));
    );
    

    Leaving aside the question of should you have them at all ;-)

    0 讨论(0)
  • 2021-01-01 12:15

    While Ed's comment is true, I do prefer actual properties over the setter/getter antipattern. When 1/3rd of the methods in your domain graph are dummy methods that have been generated by eclipse, there's something wrong.

    Without first class properties, however, I believe the antipattern makes the most sense.

    Furthermore, it makes code completion easier.

    obj.set (control shift space) for setters
    obj.get (control shift space) for getters

    0 讨论(0)
提交回复
热议问题