Re: accessor generation???
Re: accessor generation???
- Subject: Re: accessor generation???
- From: Matt Neuburg <email@hidden>
- Date: Tue, 9 Jul 2002 07:38:45 -0700
On Fri, 5 Jul 2002 13:52:40 +0200, Ondra Cada (or possibly Ondra Hada, no
one really knows) said:
>
Perhaps the problem is that I use some wrong design principles, but in 15+
>
years of object programming in ObjC I've needed plain accessors of type
>
-blah { return blah; }
>
-(void)setBlah:b { [blah autorelease]; blah=[b retain]; }
>
so rarely, that using a generator for them would be utterly ridiculous
>
---
>
Ondra Hada
>
OCSoftware: email@hidden http://www.ocs.cz
I agree that this pattern starts to go away after awhile, but as a relative
newbie I can answer from the beginner's point of view. I think the books
are right to start by teaching this pattern. (Except, of course, when they
teach it incorrectly, like Garfinkel and Mahoney.)
One problem - the biggest problem - is memory management. Beginners find
this confusing. If I've got an ivar that's an object pointer, then when I
set it I've got to remember to retain the object. If I don't want a leak,
I've got to remember to release the existing object. Passing through a
setter relieves me of the worry of remembering to do this or figuring out
whether it has been done, so I don't end up with too many releases or too
many retains. This makes a huge difference when you're just starting out
and sigbussing your way down in flames all the time (or, contrariwise,
leaking out the wazoo).
Another problem is code legibility. Referring to ivars implicitly can be
confusing ("what is this thing?"). It's clearer to be explicit, which you
can do by saying self->ivar or by using an accessor.
Another problem is privacy. Sometimes you just want to expose a getter, or
both a getter and setter; sometimes neither. The matter can be neatly
clarified by putting the getter or setter in the .h file or in the .m file
(in a category), as appropriate.
Another problem is key-value encoding; if you want to take advantage of
this, you need accessors (assuming the ivars themselves are private).
So as a pedagogical tool and a solid starting point, I'd say the simple
accessor pattern is a good one. m.
--
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
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.