Re: Should a Method set NSError* to nil when No Error?
Re: Should a Method set NSError* to nil when No Error?
- Subject: Re: Should a Method set NSError* to nil when No Error?
- From: Jerry Krinock <email@hidden>
- Date: Sat, 19 Apr 2008 13:10:37 -0700
On 2008 Apr, 19, at 11:24, Bill Bumgarner wrote:
If there is no error -- if the return value is set and valid -- then
the behavior regarding the (NSError**) argument is undefined.
Agreed, although not the way I would have designed it.
Any caller relying upon the value to be...unchanged...is implemented
incorrectly.
Thank you! That's what I wanted some support for.
As such, you are certainly free to do something like:
...
*anError = [NSError ...]; // Whoops
...
Except that the above line will cause a crash if the invoker was not
interested in the details and passed anError = NULL.
Forgetting to test that anError != NULL before assigning it is a
really easy mistake to make (oh, especially when coding in Mail, I
know...but I've done it in actual work!) Also, it is a mistake that
won't show up in testing unless the test suite covers the triggering
error. Ouch.
So, as of 2 hours ago I'm using the following macro at the beginning
of all my methods that take an NSError** argument:
#define SSYInitErrorP(_error_p) NSError* dummyError ; \
if (_error_p == NULL) { \
_error_p = &dummyError ; \
} \
* _error_p = nil ;
I use it like this, with an (NSError**)error_p:
SSYInitErrorP(error_p)
Besides setting *error_p to nil, which we agree is not necessary but I
"just like", it also assigns error_p to a dummy NSError* if NULL was
passed in, avoiding latent crashes.
As the caller of your own method, do not rely on *anError being set
to nil. Doing so creates an impedance mismatch with the rest of
Cocoa's behaviors and, thus, is simply asking for trouble.
I agree with that. Thanks again, Bill.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden