Re: Object-C question #1
Re: Object-C question #1
- Subject: Re: Object-C question #1
- From: Shawn Erickson <email@hidden>
- Date: Wed, 8 Dec 2004 08:56:35 -0800
On Dec 7, 2004, at 11:11 PM, M. Uli Kusterer wrote:
-(id) initWithString: (NSString*)string
{
if( !(self = [super init]) )
return nil;
[self parseString: string];
return self;
}
Catches the same semantics, and IMHO makes the code more readable if
you have a lot of initialization statements.
Actually if I use this form I much preferred to test against nil
instead of using the ! operator (actually I always test against nil
when dealing with object references/pointer). I find that clearer to
understand when scanning code visually, etc. Also I often try to set
the variable outside of the if statement clause because doing so can
make it easier to debug symbolically.
-(id) initWithString: (NSString*)string
{
self = [super init];
if (self == nil) // or "if (nil == self)" if you are one of those types
return nil;
[self parseString: string];
return self;
}
Or how about this fun way if you want to have one function exist point
and avoid deep nesting, etc. (/me puts on flame suit)...
-(id) initWithString:(NSString*)string
{
do {
self = [super init];
if (self == nil) break;
[self parseString:string];
if (someBadThingHappened) {
[self release]; self = nil;
break;
}
[self someOtherValue:value];
} while(0);
return self;
}
Or with those "evil" gotos...
-(id) initWithString:(NSString*)string
{
self = [super init];
if (self == nil) return nil;
[self parseString:string];
if (someBadThingHappened) goto FAILED;
[self someOtherValue:value];
if (someOtherBadThingHappened) goto FAILED;
return self;
FAILED:
[self release]; self = nil;
return self;
}
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden