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…

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.