Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Programming Public fields vs. properties YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

Public fields vs. properties
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

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Programming

VB.NET


Related pages

AssertWasCalled and Methods Called Multiple Times

AssertWas[Not]Called and Object Properties

Rhino Mocks's AssertWasNotCalled

TFS - The underlying connection was closed: an unexpected error occurred on a receive.

PrivateObject and Out/ByRef parameters

WCF - The underlying connection was closed: an unexpected error occurred on a receive.

PrivateObject, WithEvents, and generics

Accessing private members of base classes

CA1800:DoNoCastUnnecessarily

PrivateObject and WithEvents

The creator of this fault did not specify a Reason.

Accessing private and protected members - PrivateObject and PrivateType

Saving / restoring window placements in .NET

Checking Property Change Notifications

Rhino Mocks's AssertWasCalled in VB.NET

First steps with Rhino Mocks (in VB.NET)

Meaningful identifiers

A Case for FreeAndNIL

Random()'s Determinism

Rounding and precision on the 8087 FPU

Controlling Conditional Defines and Compilation Switches

Registering Extensions

Accessing Protected Members