On Thursday, 11 December 2014 10:09:28 UTC+2, JiiPee wrote:
> On 11/12/2014 07:04, Paavo Helde wrote:
> > JiiPee <
n...@notvalid.com> wrote in news:2l6iw.166444$td6....@fx30.am4:
> >
> >> On 11/12/2014 01:11, Ian Collins wrote:
> >>> Care to explain "can be debugged"?
> >>>
> >> void Point::setX(int x)
> >> {
> >> m_x = x;
> >> }
> >>
> >> Lets say we have a very large code and we know that the problem is
> >> that some line is setting x to zero (we cannot divide by zero). We do
> >> not know who set it to 0.
> > Debugging can be done with a public variable as well, just set up a data
> > breakpoint on the needed memory address, with a suitable condition.
>
> oh, I have never done that myself. I ll have to check that...So why
> Sutter and guys then say debugging is easier with functions?Surely they
> know what you said...
> But lets say you want to print/log to a file also something when x==0,
> that would not be possible I guess? For that we would need the setX
> -function.
Indeed, if you want to write some run-time sanity checks or debug
tracing or logging for (at least some of) occasions when value of
member changes (or is read) then that sort of debugging is easiest to
do with accessor functions. I trust that is what Sutter had in mind.
> "In general, public variables can be used if there are no constraints or
> class invariants. With a point class, this would mean that x and y are
> independent and can take any values. If zero values are prohibited, this
> is already an invariant and instead of a public variable there should be
> getters and setters which assert the invariant. Or alternatively,
> instead of the plain int the public member variables could be of a
> user-defined class like NonZeroInt, which have their own assignment and
> conversion operators checking this invariant. For libraries, it is also
> important to make the usage as simple as possible, and to avoid any
> suspicions (unfounded or not) that there might appear performance
> problems. So I am not surprised they are using public members."
>
> This makes sense, and I am also taking that route to putting them as
> publics. I guess one has to find a balance when to do it and when
> not.... plus I do not think there is any definite truth here. Its a bit
> a matter of taste as well I guess. But if most of the professionals use
> in libs publics here, then I guess its good to follow them...they all
> have been thinking about this and did their desisions.
On general case neither public accessor member functions nor public
member data do make sense. Every mutable member is usually related
with rest of the data that is managed by class. There are always
dependencies. Exposing it results that the class has effectively
dropped responsibility to manage its data.
For example about such dependencies imagine that "std::vector" had ...
3 getters: 'get_start()', 'get_finish()', 'get_end_of_storage()'
and 3 setters: 'set_start(s)', 'set_finish(f)', 'set_end_of_storage(e)'.
That is it. What else you need? It is all data members it has.
Indeed, such "vector" would be worthless. The classes that have lot
of getters and setters tend to be as worthless as rule.