Yes, I see I think. You're saying you can incorporate "error
checking" such as range checking in your "set" portion of your
property. I do that all the time. But I do that so that when you use
the public portion (the Name in my example, not the 'name'), you can
get error checking. I'm still free to use .name internally to the
class as I see fit.
Indeed you are. The point is that if you gateway everything through
the property, you have clear code that is easy to understand at a
glance, and is guaranteed to be consistent because the modification of
the private backing variable is *always* done by the exact same code.
If you've the variable declared as a private variable and you write
properties for use by external interfaces and you modify the variable
internally without using the properties, that is certainly your right.
All that I am saying is that this has the decent chance (particularly
in a multi-programmer environment) to be badly used since you are then
able to bypass the properties and introduce potentially inconsistent
behavior. Like all practices, however, whether or not to use them is
situationally dependent. There is always the exception to a given rule
or practice.
"If you don't have any reason"...well, that's my point--I do have a
reason. I use Properties (perhaps improperly) as a gateway or
interface with the outside world, outside my class, only. Not
internally.
Okay. I said that only because I haven't seen any reason thus far in
this thread to do it any other way. Simple preference might be reason
enough for you. It's not for me. It's always easier to have access to
the variable directly instead of having a property in the way. But,
the moment you start needing to use the property for logic, you have to
refactor the code. If you factor it properly in the beginning, you
avoid having to refactor for that reason later, and the code speaks for
itself (because it says "It is impossible for me to be changed without
going through this property, so you can be assured that I won't be
modified elsewhere in the class," which also can help you to understand
your code more quickly 6 months later, as well as someone new to learn
it the first time around looking at it).
There are times where using a private variable directly is something
that makes a class easier to implement. There are also ways around
that, you can use the property setter to store a second copy of the
value in a private variable that otherwise has no relation to the
property. You can also use methods to format values going back out to
the application. One of the great things about programming is that
there are multiple avenues to go down and one just has to determine
what is the best one for every situation.
Think about an example where you're storing a social security number in
a database. Let's say that you need to write a class that represents a
person, and that class needs to track the person's first name, middle
name, last name, and social security number. Usually, when you pull
the SSN out of the class, you're going to want it formatted in the
usual XXX-XX-XXXX format, not as a series of 9 digits. When you set
the value, you would accept either/or, when you get it out, you'd
output just the one that you'd use for formatting purposes. (This is,
itself, an example of accepting flexible output and strict about
output, which is a basic maxim of writing robust software.)
So, the class stores the social security number as nine characters.
Internally to .NET this means that its using 18 bytes, though when you
send it to a properly configured database system, it'll be back at 9.
(Internally, .NET encodes strings in UTF-16, like Win32 and Java do.)
A database system will usually be designed to use UTF-8 because it
saves a great deal of space any time you have a non-minority of Roman
characters stored within it (that is, characters which fit in the 7-bit
ASCII encoding). You could send the SSN to the database server as 11
characters, or as 9 characters. Now, specifically for SSNs you could
just filter out all characters that do not fit the character pattern
'[0-9]', and that'd work. You could also have a property that is
backed by a private variable that is accessible and just read that
variable. Or, you could have a private variable that holds a copy of
the value being held in the *real* backing store for the property and
just use that when you're writing to the database.
In *this* case, I would probably read the property, strip the
punctuation, and send the sanitized output of the stripping operation
to the database. I wouldn't use an accessible private variable at
all. But if you have lots of properties that need some sort of
processing before they're shunted to a database, it can be easier to
have the setter for the property put the information in another private
variable that is accessible, or even a nested structure that belongs to
the class that is used to hold such things. It all depends on what
you're doing and what justification there is for doing it that way.
Another time that not using automatically implemented properties is
worthwhile is when you're implementing the property in order to get/set
some dynamic value that is calculated, of course; not only will you be
unable to use an automatically implemented property for that purpose,
but there will be no backing store that is directly linked to the
property, so it wouldn't make any sense.
Well that's news to me...the part about "internally". Externally,
like I said, I already use properties as a gateway.
Is what you posted above "conventional" in C# or something you like to
do?
Above? I assume you mean the post I made which created another
subthread; not sure how Google Groups views it, I read Usenet with a
threaded newsreader.
The example I posted was just that, an example. In the software that I
write in C#, I largely use that style of property because it is
efficient and because it makes it impossible for some method in the
class to go change the variable backing the property, making life
easier since it proactively eliminates many subtle bugs simply by
making it impossible for them to exist in the first place.
Again, to each their own. Though, to clarify, when I say that it's
best to avoid doing something unless there is a reason for doing so, I
really mean "it's best to avoid doing it unless there is a compelling
logical reason for doing so." I wasn't talking about preference or
some decision arrived at a long time ago that has never since been
questioned. I do think that it's best that we all question our
practices as programmers every now and again... The realm of
programming has very little room for dogma.
I am not saying that using automatically implemented properties is
something that should be done exclusively, rather that automatically
implemented properties make life easier generally because they hide the
private variable from the programming, and that reduces the probability
that something bad could happen since the setter exclusively controls
the private variable (unless, that is, you have a part of the class
implemented in IL Assembly, which I don't think will give you such
protection, but that's another can of worms altogether).
HTH,
Mike