Re: Q: how do you generate get/set methods?
Re: Q: how do you generate get/set methods?
- Subject: Re: Q: how do you generate get/set methods?
- From: Karl Goiser <email@hidden>
- Date: Wed, 21 Jan 2004 23:53:40 +1100
G'Day mmalc,
Sir, I think you have fundamentally missed my point! From other
comments it was probably that my writing was unclear:
It was not my intention to argue that instance variables should not
have public accessor methods. As you point out, model objects are a
trivial example of a valid case. I was trying to argue that public
accessor methods are good where the design warrants, but that automatic
creation of public accessor methods opens the object totally and breaks
the principle of encapsulation.
I guess I have this horror scenario in my head where someone would
write a script which would write public getter and setter methods for
every class in a project and ending up with object-oriented's
equivalent of goto spaghetti code...
What would be Wrong would be if external objects modified an instance
variable directly [...] -- *this* is what would be a violation of the
principle of encapsulation.
Design free instantiation of public getter and setter methods allow
precisely this violation.
The other important issue in Cocoa is memory management. Even if
accessor methods are not publicly exposed, they serve a very useful
purpose in facilitating memory management. If you litter your code
with calls to retain and release to ensure longevity of an instance
variable (in contrast to assuring that a locally-used variable does
not disappear), then you are likely to make mistakes. Calling your
own accessor methods to set instance variables significantly reduces
the likelihood of error.
Yes, you (and others off list) are right. I was thinking about
'public' accessor methods during the writing and should have specified
it so.
Sorry for the confusion.
Karl
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.