Re: fields or properties
- From: "Michael S" <a@xxx>
- Date: Thu, 14 Apr 2005 14:22:36 +0200
One of the most long-lived dogma in the OO-purists camp is to never expose
fields as public.
However, I think there can be benefits in having public fields.
Consider a class Person with a public int property of Age and a public
string field called Name.
What the class Person tells you is that the Age property is somewhat
managed, perhaps the property validates that an age isn't a negative number
and so forth. The property protects you in some manner.
But the class also tells you that the Name field is not handled at all. The
Person class won't mind if you serialize an mp3-file to a hexstring and use
that as a name, hence it is your responsebility to check that input is
meaningful.
By going by the idiom of sporting all fields private and have public getters
and setters for all those fields, is extra work and bloats your code. But
what I think is worse is having Name as a property, which implies that it
validates input, while in fact it doesn't.
However, in some situations, like building user controls in asp.net; you
must sport all your fields as properties.
Hope this helps
- Michael S
.
- Follow-Ups:
- Re: fields or properties
- From: Jon Skeet [C# MVP]
- Re: fields or properties
- From: glenn
- Re: fields or properties
- References:
- fields or properties
- From: julien
- Re: fields or properties
- From: glenn
- fields or properties
- Prev by Date: Change Properties Works In FrmLoad but not constructor
- Next by Date: how to check if a integer is even or odd ?
- Previous by thread: Re: fields or properties
- Next by thread: Re: fields or properties
- Index(es):
Relevant Pages
|
Loading