On Apr 4, 2012, at 1:38 PM, Ariel Feinerman wrote:

> Hi,
> 
> I think the question was asked and answered but I cannot see a clearance
> what is the right way to write init in the cases the arguments are nil or
> wrong?
> Returning a nil or raising an exception?

Well first, this is my personal opinion how to deal with this, but others may 
think differently:


Assuming we don't know anything about a client who invokes the init method - 
which is often a reasonable assumption, then I think, you have not much options:

In a "debug" version, use NSAssert and friends (ensure Preprocessor macro 
"NS_BLOCK_ASSERTIONS" is not defined). Use Unit tests in order to detect *any* 
possible logic error. In a "release" version where NSAsserts and friends may 
become no ops, this may possibly ensure that no invalid parameters will be ever 
passed to the method. 

However, if you cannot guarantee that - or if you find this assumption too 
hairy - the best you probably can do is:

if (url == nil) {
    NSLog(@"FATAL ERROR: invalid parameter in initWithURL:");
    abort();
}


This may sound harsh, but otherwise, we don't know how a client would deal with 
a nil result. If your init methods is well documented, and the client would 
pass invalid parameters anyway, it is very likely that the client is NOT 
properly prepared to deal with unexpected results. Likely, it doesn't know that 
the parameter is invalid - because of another bug.

So, we can almost be certain, that a client would not expect anything to fail 
in this regard. So, subsequently possibly really bad things may happen.

Don't throw an exception, this only obfuscates the error, and may cause even 
more harm. Cocoa is not exception safe, which means that after an exception has 
been thrown, the program is likely in a corrupt state.

And on Mac OS X - as opposed on iOS - an exception is not properly (IMHO) 
handled anyway: the event hander just logs a message and then happily continues 
to handle events, guaranteeing for high chances that more bad things will 
happen.


Regards
Andreas

> 
> - (id) initWithURL: (NSURL *) url {
> 
> 
> if ((self = [super init])) {
> 
> if (!url) {
> 
> // ?
> 
> }
> 
> if (![url isFileURL]) {
> 
> // ?
> 
> }
> 
> }
> 
> return self;
> 
> }
> 
> I can frequently see the following in init
> 
> NSAssert(url, @"url may not be nil", );
> 
> -- 
> best regards
> Ariel
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/agrosam%40onlinehome.de
> 
> This email sent to agro...@onlinehome.de


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to