What's all this hype about hiding class fields behind properties that directly access those fields?
Private _parameters As Object
Public Property Parameters() As Object
Get
Return _parameters
End Get
Set(ByVal value As Object)
_parameters = value
End Set
End Property
Is having 10 lines of code so much better / cleaner / more politically correct than just declaring that field as public?
Public Parameters As Object
After all, the first code snippet doesn't encapsulate the class's internal data structures not one single bit.
It exposes them directly, but uses this new (well, maybe not so new) fancy paradigm:
Property Accessors (let's now all stand, wave our flags, and cheer).
I guess it's so that it looks like we're doing good and proper OOP...
Sometimes you may just want to give direct access to some of the fields of a class
(especially with classes that work more like structures).
But, if that's the case, why not just make those fields initially public?
When you later decide that you want side effects when settings / getting that field's value
(which, most of the time, is so overused, it makes me want to puke),
then you can switch the class's definition to the first one without changing a single line in the rest of your code base...
Geez, people, let's KISS!
Update
An interesting twist to the story - shorthand property declarations in VB.NET 2010:
Public Property Parameters As Object
Basically, what this concedes, IMO, is that there's no real difference (as far as design goes)
between public fields and public properties that directly access private fields... :-)
Top
|