Mut...@dastardlyhq.com wrote:
> I do the opposite. Make everything public then set things private if they
> absolutely must be. I don't understand why you'd take away the ability of
> something to access as much of your class data as possible, its not your
> house that needs to be protected against theft, its code providing a service.
Because the more you expose the private implementation of a class, the
harder it becomes to refactor without breaking existing code.
You might think: "Sure, perhaps refactoring the implementation of this
class might cause me to have to change the five million places where
the class is being used, but so what? The compiler will help me with
that. I'll just use five hours to fix every place and I'll be fine."
However, that approach doesn't work if you are making a library that's
used by several projects, even ones you aren't developing yourself.
If you break the API or usage of your class, ie. break backwards
compatibility, you may be causing a lot of work to other people who
are using your class.
Just as a very small and simple example of how a private implementation
is hidden from a public interface: Do you know how std::vector::size()
is implemented? There is, in fact, more than one way to implement it.
For example, the number of elements could be a member variable, or the
function could calculate it as the difference of two pointers.
Which one is it? You don't need nor have to know! The implementation
can choose whichever way it wants. And if it wants to change the
implementation it won't break any code.
(And if you think "that's a stupid example, nobody implements it
as the difference of two pointers", you'd be wrong.)