Re: argument checks
Re: argument checks
- Subject: Re: argument checks
- From: Graham Cox <email@hidden>
- Date: Thu, 12 Jun 2008 22:01:23 +1000
On 12 Jun 2008, at 5:47 pm, Chris Suter wrote:
Can you give me a counter-example?
Let’s say the function your calling uses one of the following:
-[NSDictionary setObject:forKey:]
-[NSMutableArray addObject:]
Raising an exception is a good response when a nil argument isn't
appropriate, but that only shows those methods are correctly dealing
with that anticipated situation. Plenty of other code uses nil to mean
"use the default", or is harmless.
+[NSDictionary dictionaryWithObjectsAndKeys:Object1, Key1,
ObjectThatIsNil, Key2, Object3, Key3, nil]
Actually that effect can be extremely handy. For example the userInfo
dict to a notification can often be set up in order such that optional
info can be nil - that terminates the list early and perfectly safely
- it can save a lot of checking and other conditional code for passing
different arguments via a dictionary. Of course you have to
deliberately make use of this feature and I can see it could catch out
the unwary. But then again, if I pass a nil object to a method that
internally puts that into a dictionary like this, and a) the method
didn't assert that it didn't accept nil and b) used it in such a
fashion blindly and c) didn't document that nil was unacceptable, I'd
call that a bug in the method, not a fault of principle.
But maybe the original point is getting lost? You asserted (pardon the
pun) that passing a nil argument was never acceptable - I say that
it's often acceptable, provided that has been anticipated correctly in
the design of the method that receives the argument, therefore one
should design methods with this possibility in mind - either by
rejecting nil, documenting that it's not OK, or working correctly
anyway. Theory and practice don't always coincide though, I'll be the
first to admit that.
G._______________________________________________
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