Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you use accessor properties from within the class, or just from outside of the class? [duplicate]

Tags:

c#

.net

I have a class 'Data' that uses a getter to access some array. If the array is null, then I want Data to access the file, fill up the array, and then return the specific value.

Now here's my question:

When creating getters and setters should you also use those same accessor properties as your way of accessing that array (in this case)? Or should you just access the array directly?

The problem I am having using the accessors from within the class is that I get infinite loops as the calling class looks for some info in Data.array, the getter finds the array null so goes to get it from the file, and that function ends up calling the getter again from within Data, array is once again null, and we're stuck in an infinite loop.

EDIT:

So is there no official stance on this? I see the wisdom in not using Accessors with file access in them, but some of you are saying to always use accessors from within a class, and others are saying to never use accessors from with the class............................................

like image 235
Alex Baranosky Avatar asked Jan 24 '09 13:01

Alex Baranosky


3 Answers

I agree with krosenvold, and want to generalize his advice a bit:

Do not use Property getters and setters for expensive operations, like reading a file or accessing the network. Use explicit function calls for the expensive operations.

Generally, users of the class will not expect that a simple property retrieval or assignment may take a lot of time.

This is also recommended in Microsoft's Framework Design Guidelines.;

Do use a method, rather than a property, in the following situations.

The operation is orders of magnitude slower than a field set would be. If you are even considering providing an asynchronous version of an operation to avoid blocking the thread, it is very likely that the operation is too expensive to be a property. In particular, operations that access the network or the file system (other than once for initialization) should most likely be methods, not properties.

like image 126
oefe Avatar answered Oct 02 '22 23:10

oefe


I think its a good idea to always use the accessors. Then if you need any special logic when getting or setting the property, you know that everything is performing that logic.

Can you post the getter and setter for one of these properties? Maybe we can help debug it.

like image 39
Jason Punyon Avatar answered Oct 03 '22 00:10

Jason Punyon


I have written a getter that opens a file and always regretted it later. Nowdays I would never solve that problem by lazy-constructing through the getter - period. There's the issue of getters with side-effects where people don't expect all kinds of crazy activity to be going on behind the getter. Furthermore you probably have to ensure thread safety, which can further pollute this code. Unit-Testing can also become slightly harder each time you do this.

Explicit construction is a much better solution than all sorts of lazy-init getters. It may be because I'm using DI frameworks that give me all of this as part of the standard usage patterns. I really try to treat construction logic as distinctly as possible and not hide too much, it makes code easier to understand.

like image 39
krosenvold Avatar answered Oct 03 '22 00:10

krosenvold