Re: Objective-C Instance Variable Names
Re: Objective-C Instance Variable Names
- Subject: Re: Objective-C Instance Variable Names
- From: "Rob Napier" <email@hidden>
- Date: Thu, 3 Apr 2008 13:58:31 -0400
On Thu, Apr 3, 2008 at 12:45 PM, Jens Alfke <email@hidden> wrote:
>
> On 3 Apr '08, at 9:29 AM, Richard Somers wrote:
>
> There is a common practice of prefixing instance variable names with "_",
> > a single underscore character.
> >
>
> And it's a very good idea to do so, IMHO. (The exact prefix isn't
> important, just as long as it's easy to distinguish ivars from local vars at
> a glance.)
Apple reserves the use of leading underscores for it's own use:
http://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html#//apple_ref/doc/uid/20001281
While they append the phrase "especially in methods," they do mean for
everything, and you can collide with them if you name your own identifiers
(including instance variables) with leading underscores.
I used to agree with your sentiment on distinguishing ivars with local vars
until I discovered the following:
1. Leading underscores really will cause you to collide with Apple and it
really did blow up in my face.
2. Leading "m" makes you look like a C++ programmer and other ObjC
programmers will laugh at you. You can take that for what it's worth, but
it's worth keeping in mind if you're going to work on large projects with
other programmers. "Other ObjC programmers will laugh at you" seems to be a
common mechanism for keeping large projects sane. Java guys use their
compiler. We use peer pressure. Which you think is a better way to enforce
good practice will determine whether you are happier as a Java or ObjC
programmer.
3. XCode 3 now provides good color coding to distinguish ivars from local
vars.
4. And besides, if you would use accessors properly, ivars should very
seldom show up in your code so you shouldn't have confusion (this being the
cause of #2 above).
Personally, I find the lack of good naming conventions for instance
variables one of the few things that annoys me in ObjC. That said, I've
found that with proper accessor usage, it doesn't really matter, and with
XCode3 you can do fine even without proper accessor usage.
But in any case, you shouldn't use underscores.
-Rob
--
Rob Napier -- Software and Security Consulting -- http://robnapier.net
_______________________________________________
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