Re: Objective-C question
Re: Objective-C question
- Subject: Re: Objective-C question
- From: Scott Thompson <email@hidden>
- Date: Wed, 5 May 2004 09:19:45 -0500
On May 4, 2004, at 7:50 PM, Ondra Cada wrote:
On 5.5.2004, at 2:19, Scott Thompson wrote:
That's not entirely true. I mean, if I write a C function and pass
it a pointer to an Objective-C object then I can access the instance
variables of the object:
void MyCFunction(ObjectiveCClassWithInstanceVariables *someObject)
{
int fred = someObject->anInstanceVariable;
}
There should be nothing wrong with that.
There is a number of potential wrongs with that, more or less all of
them harping on the subject of "broken encapsulation". At the very
least, I would recommend using KVC instead (unless your function is
pretty low-level and very often-called).
I happen to agree with you.
But playing the Devil's advocate, it is also the case, however, that
Encapsulation is "just" a design tool that can be applied to the class.
Potentially the ObjectiveCClassWithInstanceVariables class could have
been designed expressly with the idea that it's "anInstanceVariable" is
always public. An example might be if one was creating creating a
class that implements a Rectangle (assume that NSRect doesn't work out
because it uses float instead of double). In that case, if the class
were used in time-critical operations, it might be important to access
the origin and size portions of the rectangle object directly instead
of involving the overhead of a method dispatch.
One might argue that if any of the members of the "rectangle class" (or
ObjectiveCClassWithInstanceVariables for that matter) are public then
why are the part of a "object" anyway... but having public instance
variables can be a useful design decision in very specialized
circumstances.
Encapsulation is a excellent tool and perfectly aplicable to 99.99% of
the cases, but I prefer to keep in mind that no such tool is perfect
and there is not a rule that says encapsulating EVERY instance variable
is always the Right Thing to do.
Scott
_______________________________________________
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.