Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone ivar naming convention [duplicate]

Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?

I've noticed that in a lot of the reference materials out there, I see that a lot of the time, variables are named _variable in the .h file, then are @synthesize'd in the .m file as

@synthesize variable = _variable;

Why is this done? What am I missing?

Thanks!

like image 240
Neal L Avatar asked Jan 22 '10 02:01

Neal L


3 Answers

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.

like image 169
Kendall Helmstetter Gelner Avatar answered Oct 26 '22 15:10

Kendall Helmstetter Gelner


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
like image 28
gerry3 Avatar answered Oct 26 '22 14:10

gerry3


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.

like image 9
Jeff Kelley Avatar answered Oct 26 '22 14:10

Jeff Kelley