Proper usage - accessors/getters vs normal methods
I am having trouble differentiating between accessors/getters, which represent object's properties and normal methods. In C++, the naming conventions for accessors and normal methods are different (e.g. Google Style guide suggests lower-case for accessors and PascalCase for normal methods). Moreover, the order of normal methods and accessors/getters (before or after) is also subject to conventions. In Java, one might have a dilemma whether a method should begin with "get" or "compute"/"find" to indicate a getter and a normal method, respectively. In C#, there is a dilemma what should be a property and what should be a method. Whenever I create a new class, I am not sure how to classify methods to normal methods and getters.
Is there an easy way to determine whether what is a property of an object / accessor/getter and what should be a normal method?
And does any of the following have something to do with differentiating between an accessor/getter and a normal method:
- Computation is cheap
- A new object (or a copy) is returned, instead of (const) reference
- There is no setter
- There is a way to influence that "property" but indirectly
To illustrate this, here is the example (in C++): Suppose I want to create a data structure which can hold a fix开发者_StackOverflow社区ed number of elements, and apart from insertion/removal operations it offers a series of methods which work on the elements contained (i.e. average, median, minimum, maximum etc.). Now, take a function which computes the average for example; one might say that this is a property of an object and re-calculate it whenever an element is inserted/removed and thus treat it as a getter like const double& average()
. The other way would be to compute it on-demand and treat it as a normal method, i.e. double ComputeAverage()
. Also, suppose there is a method which returns a set of contained elements; it could be treated as a getter const set<int>& unique_elements()
if there is no need to compute it every time, but if the class computes it every time then the set<int> ComputeUniqueElements()
would be more appropriate.
Creating a separation between accessors and "normal methods" is a bad idea. A method is a method; it's a member function that has some specific effect. The fact that a member function simply sets the value of a member variable, or returns the value of a member variable, is an implementation detail.
And a good API isolates the outside world from implementation details. The outside world should neither know nor care that a particular function is just an accessor, if for no other reason than the fact that this can change.
Let's say you have a name
"property" in your class. Originally, you store it as a std::string
. You provide functions to get and modify the name. All well and good.
Now, let's say that you decide to change how the name is stored. You need to parse the name into a first name and a last name. Does the outside world need to know you're doing this? The name of your setter method doesn't need to change. It's interface doesn't need to change. All that needs to change is how you implement the function.
In C++, the naming conventions for accessors and normal methods are different (e.g. Google Style guide suggests lower-case for accessors and PascalCase for normal methods).
Google's Style guide does not represent C++ or its programmers; it only represents what Google does.
精彩评论