On Thu, 4 Jul 2002 13:48:35 -0400, "Robert Chapman"
>I understand what you mean when you talk about the value of testing.
>However, I cannot find any way to believe that public variables is a good
>solution. I normally use private member variables that can be retrieved or
>set via property of method calls. Everything I have ever read, going back
>to Code Complete, indicates that protecting your data is very important,
>public variables violates that tenant.
What does 'private' mean? It means that we don't want others messing
with the variables. We can achieve that goal by using comments, or by
making the name of the variable indicate privacy.
/*private*/ public double myVariable;
This is pretty clear. The compiler won't enforce it, but anybody who
wants to use that variable will see the 'private' in front of it, and
realize that they shouldn't mess.
public double myPrivateVariable;
This is also pretty clear. In some ways it's better than the comment
convention since anybody who uses is must type 'private' every time
they make use of it. If they didn't get a guilty conscience about
using the commented variable, they'd likely get one using the
privately named variable.
You may be concerned that the compiler is not able to enforce privacy
in these instances. However, anybody who really wants to gain access
to a private variable can always do so by one means or another -- so
the compiler enforcement really isn't that valuable.
Yes, I know this flies in the face of a decade of OO dogma, but this
is a particular dogma that I think needs a bit of tuning.
Robert C. Martin | "Uncle Bob"
PO Box 5757 | Tel: (800) 338-6716
565 Lakeview Pkwy | Fax: (847) 573-1658 | www.objectmentor.com
Suite 135 | | www.XProgramming.com
Vernon Hills, IL, | Training and Mentoring | www.junit.org
60061 | OO, XP, Java, C++, Python |
You and I can enjoy the experience of not always seeing
eye-to-eye, but we can also respect each other, even when
you might find some idea of mine totally ludicrous.
-- Richard Riehle