Re: [OT] Retain count riddle
Re: [OT] Retain count riddle
- Subject: Re: [OT] Retain count riddle
- From: Chris Hanson <email@hidden>
- Date: Wed, 31 Mar 2004 15:24:44 -0600
On Mar 30, 2004, at 1:49 PM, Sailesh Agrawal wrote:
#define SAFE_SET(old, new) [new retain]; [old release]; old = new;
...
- (void) foo : (NSString*)path {
SAFE_SET(m_Path, [path lastPathComponent]);
}
There are three style issues I'd bring up with this code.
First, I wouldn't use a setter macro. I'd actually write out (or have
generated) the actual setter code. I find it's easier to maintain code
that uses the actual code instead of macros.
Second, I wouldn't use MFC-style naming of instance variables. It
doesn't play nicely with key-value coding. (Fortunately, I can at
least commend you on not using Hungarian notation!)
Finally, you're doing work in a setter. That's the cause of the actual
bug, but it's also a stylistic issue. I prefer to do as little work as
possible in setters, and to make it clear what work is being done if
it's anything other than (say) a -setNeedsDisplay: or (before bindings)
a value-changed notification broadcast.
So if I were writing this, it'd look like:
// this is "foo" above
- (void)setPathToLastPathComponentOfPath:(NSString
*)fullOrPartialPath
{
[self setPath:[fullOrPartialPath lastPathComponent]];
}
// this is the "path" setter
- (void)setPath:(NSString *)aPath
{
if (path != aPath) {
NSString *temp = [aPath copy];
[path release];
path = temp;
}
}
Yeah, it's a little more verbose, but I think the intent's clearer.
And the path setter could be generated by a tool.
-- Chris
--
Chris Hanson <email@hidden>
http://www.livejournal.com/users/chanson/
_______________________________________________
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.