Re: Why are Set & Get important?
Immortal Nephi wrote:
Tens of member functions or
hundreds of member functions modify data members.
I'd say that if you have tens or even hundreds of accessor functions,
your class design is flawed.
The general principle of class design is that its public interface
should *abstract* away the actual internal implementation of the class.
In other words, in an optimal case, one should not be able to deduce
from the public interface alone what kind of member variables the class has.
Explicit variable getter and setter functions somewhat break this
principle, as they expose the internal structure of the class. Of course
it's better than having the member variable in the public interface
directly, but it's still often a sign of flawed design.
This isn't a hard rule, of course. There are some situations where
explicit getters and setters are justifiable. However, it's a good
generic principle. Also, in general, the more explicit getters and
setters you have in a class, the more it is a sign of bad design.
As an example, consider the size() member function of the STL data
containers. Is it a getter function? In fact, it isn't: It's simply a
function to query the data container for the amount of elements stored.
It *might* be a direct getter for a member variable, but it might very
well not be (eg. many std::vector implementations don't have any
explicit size member variable, and instead size() returns the difference
between end() and begin()).
When you want information out of a class, the functions to query for
that information should be at a slightly higher abstraction level than
"return me the value of this member variable of yours".