Tuesday 3 July 2007

Properties Vs Fields

Having talked about Automatic Properties in my last post, i think its important to enter the old Properties vs Fields argument.

Arguments for making all fields private, and using properties:

1) If you have to promote a field to a property any dependant classes will require a recompile.

2) If you wish to use databinding, you need to use a property.

Arguments for using fields rather than properties

1) Slightly better performance (direct access), rather than access through an accessor method

2) More often than not you don't databind directly to a property (so only promote when required)

3) More often than not when we build and release, we build and release all components (unless you are a component developer_

4) More maintainable code (easier to look at than great big hulking property declarations, (not really an issue in a world of automatic properties)

5) Lazy Coding.


I think my preference is fairly clear (properties and automatic properties are good), but I understand if you want to use fields.

I think in a world of automatic properties, fields will not be used as often. I believe this because most coders are lazy (me included). Since automatic properties are so easy to implement there is less reason not use them (apart from those with strong opinions on using fields).

No comments: