5

Possible Duplicate:
When do you make an underscore in front of an instance variable?

As in Objective C, instance variable are protected by default, what are your preferred way to name it?

Assume you have an variable name, the following 3 ways have their supporters.

  1. _foo
  2. foo_
  3. foo
Community
  • 1
  • 1
Howard
  • 19,215
  • 35
  • 112
  • 184
  • Duplicates [1](http://stackoverflow.com/questions/5620658/does-naming-an-instance-varible-with-underscore-as-a-prefix-have-any-side-effects) [2](http://stackoverflow.com/questions/5582448/underscore-prefix-on-property-name) [3](http://stackoverflow.com/questions/822487/how-does-an-underscore-in-front-of-a-variable-in-a-cocoa-objective-c-class-work) [...](http://stackoverflow.com/search?q=%5Bobjc%5D+underscore+variable+name) – Black Frog Apr 15 '11 at 21:18

5 Answers5

6

foo. I have always disdained the _foo or foo_ styles.

jer
  • 20,094
  • 5
  • 45
  • 69
Todd Hopkinson
  • 6,803
  • 5
  • 32
  • 34
2

Apple's Coding Guidelines for Cocoa suggest you avoid an underscore prefix:

Avoid the use of the underscore character as a prefix meaning private, especially in methods. Apple reserves the use of this convention. Use by third parties could result in name-space collisions; they might unwittingly override an existing private method with one of their own, with disastrous consequences.

and since I'm not aware of any trailing underscore convention, I don't know why you shouldn't use just foo.

Jano
  • 62,815
  • 21
  • 164
  • 192
  • 1
    there is a trailing underscore convention. Google recommends it in their guide, [have a look here](http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml) – Rad'Val Apr 15 '11 at 17:03
  • I just wanted to point out that Google now follows Apple's style guide in regards to the underscore convention. – Stephen Melvin Jan 04 '13 at 22:26
1

You are not supposed to use underscore as a prefix as per _foo - that is reserved for Apple (and keeps you from accidentally re-defining a variable you do not know about!)

I like foo_, but only if you are writing accessors. Otherwise I just use foo. However for memory uses alone, it's good practice to always use accessors and just declare the ones you do not want public in a private category in the implementation like so:

@interface MyClass ()
@property (nonatomic, retain) NSArray *myArray;
@end

@implementation
@synthesize myArray = myArray_;
@synthesize myPublicString = myPublicString_;

- (void) dealloc
{
   [myArray_ release]; myArray_ = nil;
   [myPublicString_ release]; myPublicString_ = nil;
}

....

@end
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • I did not like this way until I started working for a company that uses this style in their code-style-guides. I got used to it and by now kind of even like it. – Till Apr 15 '11 at 18:53
  • I don't really like using "_" either, but I do like avoiding memory problems by always using accessors. The utility of the better memory management overrides for me the ugly in using the "-" (which you mostly do not see except in dealloc). – Kendall Helmstetter Gelner Apr 15 '11 at 19:54
0

In general terms, why would anyone name their private variables with any suffix or prefix, most of the modern day tools do a great job in colorize them differently anyway. I prefer simply foo.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
0

Since I seem to be in the minority, I'll go ahead and throw in my two cents in the interest of balance:

I like the underscore varieties. I think it makes the code semantics more readable at a glance. Sure, syntax highlighting can do that as well, but I've always preferred the IDE/theme independent approach. Once you're used to it, it's nice to have the little guy immediately set off the "directly accessing an ivar here" alarm in your head.

Obviously, this is incredibly subjective.

As an addendum, the underscore prefix is really only especially dangerous with method names, where collisions won't be immediately exposed by the compiler, but it is probably still better to go with the suffix, if you're an underscore fan.

Matt Wilding
  • 20,115
  • 3
  • 67
  • 95