Re: Accessor methods and (auto)release: conclusion
Re: Accessor methods and (auto)release: conclusion
- Subject: Re: Accessor methods and (auto)release: conclusion
- From: Marco Scheurer <email@hidden>
- Date: Tue, 6 Aug 2002 19:07:43 +0200
On Tuesday, August 6, 2002, at 06:28 pm, Ondra Cada wrote:
id s=[[doc window] title];
[doc save];
if ([s length]) ... CRASH! WOW!
As I said, but it looks like you prefer to ignore it, not crashing does
make the code correct. The correct(*) code in that case would one of:
id s = [[[doc window] title] copy];
[doc save];
if ([s length])....
or
int x = [[[doc window] title] length];
[doc save];
if (x)...
if you're interested by the original title; or
[doc save]
if ([[[doc window] title] length])...
if you're interested by the actual title.
(*) except for the fact that what is probably intended (checking if the
string is not empty) should be done with ![s isEqualToString:@""],
instead of [s length].
All you get with your accessor pattern is that your code does not crash,
but you can't be sure that "s" is the string you care about (it's
probably not). So you've got another, harder to detect bug. In my code,
what "s" I expect is specified, and there should be no surprises.
Marco Scheurer
Sen:te, Lausanne, Switzerland
http://www.sente.ch
_______________________________________________
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.