iPhone ivar naming convention [duplicate]

Solution 1:

There is not consensus on this. Some people like to use it for clarity to separate out class variables, and as another responder noted to avoid conflict with incoming parameter names. Even in Apple sample code the use is mixed.

However, I greatly prefer to not use the _ prefix and have two strong reasons:

1) Some people think the _ is a good indicator of "private". My take is that NO class local variable should be accessed without a setter/getter (property) and thus they are ALL private - given that why not name them in a way easier to read and use autocomplete on? Any overlap in names from parameters is quickly revealed by the compiler, and avoided through more thoughtful naming of parameters (or internal variables).

2) (even better reason) - if you use "refactor" in XCode on an internal class var that is named the same as the property used to access it, the property and synthesize statement will also be renamed. If you use refactor on a class variable prefixed with an _, the property name will not be changed - just the synthesize mapping to the internal name. I pretty much never want the name to vary from the property to the real variable it exposes access to. That alone makes me never want to use _ as a variable prefix, since being able to shift names is just about the most useful thing you can do to improve code clarity.

Solution 2:

Using that syntax is an option to make it more clear that the ivar and property are different things.

To code external to the class, there is no difference since it uses the property.

For code in the implementation of the class itself, it can make it more clear when the ivar is used versus the property.

For example, say we have an ivar/property for an NSNumber object:

@interface MyClass : NSObject {
    NSNumber *num;
}
@property (nonatomic, retain) NSNumber *num;
- (void)doSomething;
@end

@implementation MyClass
@synthesize num;

- (void)doSomething {
    // set the property, num is properly retained
    self.num = [NSNumber numberWithInteger:1];

    // accidentally set the ivar, num is NOT retained
    num = [NSNumber numberWithInteger:2];
}
@end

and now using a different name for the ivar and property:

@interface MyClass : NSObject {
    NSNumber *i_num;
}
@property (nonatomic, retain) NSNumber *num;
- (void)doSomething;
@end

@implementation MyClass
@synthesize num = i_num;

- (void)doSomething {
    // set the property, num is properly retained
    self.num = [NSNumber numberWithInteger:1];

    // compiler error, there is no ivar named "num"
    num = [NSNumber numberWithInteger:2];

    // set the ivar, so it needs to be a retained object
    i_num = [[NSNumber alloc] initWithInteger:3];
}
@end

Solution 3:

Previous answers are missing the history behind this. Before Objective-C 2.0, there were no properties. So you’d have an object with instance variables like this:

@interface MyObject: NSObject {
    NSArray *myArray;
}

@end

But how would you access them from other objects? the solution was to make setters and getters. But to avoid confusion, they would do it like this:

@interface MyObject: NSObject {
    NSArray *_myArray;
}

- (NSArray *)myArray;
- (void)setMyArray:(NSArray *)myArray;

@end

The _ serves to clear up confusion between the instance variable _myArray and the method -myArray.

Solution 4:

Sometimes people use mVarName (C++) and in Obj-c the style seems to be _varName. One problem you can have, is imagine that your argument to a function is ...set:(int) x - BUT - you have an iVar called x...well your going to get the compiler crying about stuff like that - not to mention its confusing.

The m,_, whatever helps to show what are member properties of the class.

 -(void) set:(int)x
{
 x = x; // x is an ivar! heh
}

VS

 -(void) set:(int)x
{
 _x = x; // ahh I see!
}