• 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: Any good advice for naming parameters of init methods?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any good advice for naming parameters of init methods?


  • Subject: Re: Any good advice for naming parameters of init methods?
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 10 Jul 2008 09:06:06 -0700


On 10 Jul '08, at 8:52 AM, Sean McBride wrote:

On 7/11/08 12:38 AM, Graham Cox said:

What I would suggest though is that you adopt a naming convention for
your instance variables (ivars) that consistently flag them as such.
Some people use a leading underscore, but Apple say not to do that.

Do the docs still say that? We've had this discussion before: <http://www.cocoabuilder.com/archive/message/cocoa/2008/4/3/203159>

With any naming convention, the possible problem is a conflict with a name in a superclass.
Apple's Cocoa frameworks tend to use a "_" prefix for both ivars and private method names.


Method-name conflicts are very bad and will cause trouble at runtime. The worst case is where Apple adds a method, in a new release of the OS, which has the same name as one of your methods. This will probably cause your app to break because when the framework calls that private method, it gets your implementation instead.

Ivar-name conflicts are, IIRC, a lot less harmful. For the most part, ivar names are used only at compile time (the generated code just accesses ivars by a byte offset from the object) so the effect in the above situation will be that your app continues to work fine, but when you try to recompile the app using the SDK for the new OS, you'll get a compile error about a duplicate ivar name. Then you just rename yours.

Someone did bring up the issue of dynamic ivar-name lookups. KVC sometimes uses these. It's possible there could be runtime problems in this case, although I'm not sure. All I can say is that I've been using the "_" prefix in my code for almost eight years now without running into such problems.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

  • Follow-Ups:
    • Re: Any good advice for naming parameters of init methods?
      • From: William Xu <email@hidden>
    • Re: Any good advice for naming parameters of init methods?
      • From: glenn andreas <email@hidden>
References: 
 >Any good advice for naming parameters of init methods? (From: an0 <email@hidden>)
 >Re: Any good advice for naming parameters of init methods? (From: Graham Cox <email@hidden>)
 >Re: Any good advice for naming parameters of init methods? (From: "Sean McBride" <email@hidden>)

  • Prev by Date: Design Question: Where to observe using KVO?
  • Next by Date: Re: NSSavePanel bug with -setNameFieldLabel: workaround?
  • Previous by thread: Re: Any good advice for naming parameters of init methods?
  • Next by thread: Re: Any good advice for naming parameters of init methods?
  • Index(es):
    • Date
    • Thread