Public Properties vs. local variables


So there was a thread recently on the aus-dotnet mailing list which piqued my interest. 

Bill McCarthy wrote (and I agree) that as a general rule you ought to always call public properties when your calling code is external to the target class/code.

As to calls or references made within a class, usually the accepted methodology is to use local references to the underlying data (backing data) source (a private variable or something else).

This leads me to an interesting crossroads.  What if there is some business logic or other functionality exposed by the properties, or triggered by events hanging off the public properties?

Playing devil’s advocate for a minute, I’d suggest that code within a class would need to be aware of the necessary business logic contained in the property and act accordingly – or – move the logic into a public function and leave properties for simple get/setters.

I’d be interested in your thoughts…


About Rob Sanders

IT Professional and TOGAF 9 certified architect with nearly two decades of industry experience, 18 years in commercial software development and 11 years in IT consulting. Check out the "About Rob" page for more information.

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>