• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: argument checks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: argument checks


  • Subject: Re: argument checks
  • From: Jason Coco <email@hidden>
  • Date: Thu, 12 Jun 2008 03:36:00 -0400

I agree with Graham, although I misread the initial question as well... if the person is passing a garbage pointer, there's really not much you can do. All you can really do is assert that the object you expected is not nil.

Why is it unsafe to pass nil? Many API in Cocoa tell you to pass nil for various default behaviors.

/jason

On Jun 12, 2008, at 03:29 , Graham Cox wrote:


On 12 Jun 2008, at 5:03 pm, Chris Suter wrote:

In the original example, myNum was being passed as a argument rather than having a message to sent to it and it’s often not safe to pass nil objects as arguments.


Hmmm... well, what's the function it's passed to going to do with it, other than call a method on it? If it's doing anything else, it's breaking other (much stronger) rules. If it tries to access its ivars directly, that's breaking encapsulation. If it tried to release it, that breaks memory management rules... etc. So I think in general it's quite safe to pass nil arguments where an object is expected - I certainly do it all the time, and haven't found any real problem with that so far.

Contractually, nil is defined as type id, and type id can be passed for any object type, so this is not even a hack - it's entirely permitted by design. NULL on the other hand, is another story.

Code that does not allow a nil argument should assert that. If it doesn't it is implicitly allowing nil, unless it is specifically documented otherwise.

Can you give me a counter-example?

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:
@gmail.com


This email sent to email@hidden

_______________________________________________

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


References: 
 >argument checks (From: Ashley Perrien <email@hidden>)
 >Re: argument checks (From: Graham Cox <email@hidden>)
 >Re: argument checks (From: Chris Suter <email@hidden>)
 >Re: argument checks (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: argument checks
  • Next by Date: Re: Issue with displaying URI prefixes on child elements using NSXML
  • Previous by thread: Re: argument checks
  • Next by thread: Re: argument checks
  • Index(es):
    • Date
    • Thread