On 2/11/2016 8:09 PM, Wouter van Ooijen wrote:
>
> [snip]
> pure getters and setters make sense when there is a significant
> chance that at some moment in the life cycle of your software, you will
> change the private data without a change in the interface. If you need
> to do this, you will be very gratefull for the abstraction of the
> getter/setters.
>
> If the chance that you will do that is (almost) zero, getters/setters
> are a nuisance, and you should instead make the attributes public.
Requirements can change.
And they do.
Bertrand Meyer thought that this was important enough to make member
function invocations look like data attribute access in Eiffel, so that
a data attribute could be replaced with a member function or vice versa
without affecting client source code. He called this the [1]“uniform
access principle”. I his words, in his 1988 book [2]“Object Oriented
Software Construction” (which really was a combined introduction to and
rationale of Eiffel, sort of like a combination of Bjarne's TCPPL and
Design & Evolution books): “All services offered by a module should be
available through a uniform notation, which does not betray whether they
are implemented through storage or through computation”.
In C++ one can implement uniform access by implementing read only
properties, but this has both a memory cost (back-pointer to the object
in each such attribute) and an execution cost (extra indirection).
I think the uniform access principle is the least weak argument for C++
language support for properties. But ideally, when a direct data
attribute is replaced with a read-only property function, that function
should be a /pure function/, possibly except caching of results, to
avoid the property-with-side-effects nightmare of e.g. properties in C#.
And the only guaranteed pure functions in current C++ are `constexpr`
functions, which is too restrictive to be useful for this.
> Non-pure getters/setters (that do something more than just
> setting/getting the attribute) are of course a different story.
Yes.
Cheers,
- Alf
Notes:
[1] <url:
https://en.wikipedia.org/wiki/Uniform_access_principle>
[2] <url:
https://en.wikipedia.org/wiki/Object-Oriented_Software_Construction>