Write-Only Properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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.
 
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.
 
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
 
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.
 
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?
 
Back
Top