"Daniel T." <danie...@earthlink.net> wrote:
Jerry Coffin <jcof...@taeus.com> wrote:
The other problem is consistency. When you need to return
something that isn't really a member variable, you use a
function call; for the member variable, you use a reference.
Isn't that exposing internal details which the client code
should not be concerned with?
IMO, you've got things exactly backwards: the user is
accessing something that _is_ a member variable, and forcing
them to access it via a function is exposing an internal
detail with which they should not be concerned.
Why is the *user* insisting that it has to be a
member-variable? What business is it of the user to decide
what bits of the object are stored and what bits are
The user isn't. The design says that this type has such and
such attributes. The design of std::list<>, for example, says
that a list has an attribute size. It's logically (read-only)
data, not a function.
In my own code, I use functions, rather than public access, for
a variety of technical reasons (and for consistency, in the
cases where the technical reasons don't actually apply). But
the naming conventions still hold: a function has a verb or verb
phrase as a name, data (such as an attribute) a noun. If the
function returns something that isn't logically an attribute of
the object, it will have a verb phrase as a name, something like
get...(), or calculate...(), or create...(). For attributes,
however, I use the convention:
Type dataName() const ;
Type /* or void */ dataName( Type const& newValue ) ;
(If it's not too expensive, and might make sense in some cases,
I'll have the setter return the old value, so that the client
code can save and restore it.)
There are reasons why you might prefer the function---I gave an
example in my latest response to Jerry, where the access had to
be virtual. But the fact remains that it is compromise, based
on technical reasons; from the design point of view, I'm dealing
with attributes, not behavior.
I agree with you. The two primary points of agreement are (1)
consistency, and (2) compromise.
The getter should not betray wither the value is stored or calculated.
used as an optimization IMHO.) Other languages have other ways. Ways
that allow the writer to distinguish data retrieval from requests.