471,344 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

Write-Only Properties

Hi,

MS Class Library Guidelines clearly discourages use of Write-Only properties
yet they're continuously availed in .NET after VB 6. Personally, I really
don't use them anywhere. Tend to find it as giving the 'highest' possible
access to data as compared to a plain read.

However, out of more than a passing interest, does anyone know of a clear
cut reason why developers are clearly discouraged from using Write-Only
properties?
Jul 21 '05 #1
5 3776
Write-only properties usually indicate a flawed design. If you are referring
to this FxCop design rule then, yes, it is not recommended to use write-only
properties because they do not server much purpose. What is the point in
asking the user to supply a value but not allow him to read it ? If a class
requires some value it is supplied through read-write properties,
constructors, method parameters and not through write-only properties because
it is not conventional. Hence, it is a flawed design.

with regards,

J.V.
"Disiac" wrote:
Hi,

MS Class Library Guidelines clearly discourages use of Write-Only properties
yet they're continuously availed in .NET after VB 6. Personally, I really
don't use them anywhere. Tend to find it as giving the 'highest' possible
access to data as compared to a plain read.

However, out of more than a passing interest, does anyone know of a clear
cut reason why developers are clearly discouraged from using Write-Only
properties?

Jul 21 '05 #2
Write-only properties usually indicate a flawed design. If you are referring
to this FxCop design rule then, yes, it is not recommended to use write-only
properties because they do not server much purpose. What is the point in
asking the user to supply a value but not allow him to read it ? If a class
requires some value it is supplied through read-write properties,
constructors, method parameters and not through write-only properties because
it is not conventional. Hence, it is a flawed design.

with regards,

J.V.

"Disiac" wrote:
Hi,

MS Class Library Guidelines clearly discourages use of Write-Only properties
yet they're continuously availed in .NET after VB 6. Personally, I really
don't use them anywhere. Tend to find it as giving the 'highest' possible
access to data as compared to a plain read.

However, out of more than a passing interest, does anyone know of a clear
cut reason why developers are clearly discouraged from using Write-Only
properties?

Jul 21 '05 #3
I agree, properties (in general) imply a 2-way or binary communication
between 2 objects and a write-only property on an object implies a 1-way or
unary communication between 2 objects.

Its considered bad form to use a write-only property because it breaks the
OO rule for properties where properties are used for 2-way communication. A
write-only property should really be made into a method call instead because
it's more appropriate since method calls imply a 1-way communication between
objects.

Besides someone might be tempted to make your write-only property into a
read-write property which would go against your initial design.

HTH,
Jorge

"Ravichandran J.V." wrote:
Write-only properties usually indicate a flawed design. If you are referring
to this FxCop design rule then, yes, it is not recommended to use write-only
properties because they do not server much purpose. What is the point in
asking the user to supply a value but not allow him to read it ? If a class
requires some value it is supplied through read-write properties,
constructors, method parameters and not through write-only properties because
it is not conventional. Hence, it is a flawed design.

with regards,

J.V.

"Disiac" wrote:
Hi,

MS Class Library Guidelines clearly discourages use of Write-Only properties
yet they're continuously availed in .NET after VB 6. Personally, I really
don't use them anywhere. Tend to find it as giving the 'highest' possible
access to data as compared to a plain read.

However, out of more than a passing interest, does anyone know of a clear
cut reason why developers are clearly discouraged from using Write-Only
properties?

Jul 21 '05 #4
The property syntax is just a nice way to make using getter and setter
accessor methods to wrap an internal field more intuitive, easy to read,
etc.
If you only want to set a value, you do not need (or want) the intuitive
feel of property syntax because it will more likely confuse developers
rather than help them. When a developer sees a property, it is natural
to expect to be able to read it after writing it.
For cases when you just need to set an internal value, it makes more
sense to create a method that takes the value as a parameter. Then
there is no inherent expectation that the consumer of your class should
be able to read the value back.

Disiac wrote:
Hi,

MS Class Library Guidelines clearly discourages use of Write-Only properties
yet they're continuously availed in .NET after VB 6. Personally, I really
don't use them anywhere. Tend to find it as giving the 'highest' possible
access to data as compared to a plain read.

However, out of more than a passing interest, does anyone know of a clear
cut reason why developers are clearly discouraged from using Write-Only
properties?

Jul 21 '05 #5
Hi,Thanks. I agree with the sentiments so far. The advocacy for Write-Only
properties on the web seems close to hinge around the following example:

"..a password property on a security object. There's no reason to read the
password back, but we would, sometimes, want to set it.

Seems the solution to the example is flawed in my mind. Whats your take?

"Joshua Flanagan" wrote:
The property syntax is just a nice way to make using getter and setter
accessor methods to wrap an internal field more intuitive, easy to read,
etc.
If you only want to set a value, you do not need (or want) the intuitive
feel of property syntax because it will more likely confuse developers
rather than help them. When a developer sees a property, it is natural
to expect to be able to read it after writing it.
For cases when you just need to set an internal value, it makes more
sense to create a method that takes the value as a parameter. Then
there is no inherent expectation that the consumer of your class should
be able to read the value back.

Disiac wrote:
Hi,

MS Class Library Guidelines clearly discourages use of Write-Only properties
yet they're continuously availed in .NET after VB 6. Personally, I really
don't use them anywhere. Tend to find it as giving the 'highest' possible
access to data as compared to a plain read.

However, out of more than a passing interest, does anyone know of a clear
cut reason why developers are clearly discouraged from using Write-Only
properties?

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bob Murdoch | last post: by
8 posts views Thread by Ben | last post: by
2 posts views Thread by bissatch | last post: by
4 posts views Thread by Tom Van Ginneken | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.