Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefixing property names with an underscore in Objective C [duplicate]

I've previously avoided underscores in my variable names, perhaps a holdover from my college Java days. So when I define a property in Objective C this is what I naturally do.

// In the header @interface Whatever {     NSString *myStringProperty }  @property (nonatomic, copy) NSString *myStringProperty;  // In the implementation @synthesize myStringProperty; 

But in almost every example it is done like

// In the header @interface Whatever {     NSString *_myStringProperty }  @property (nonatomic, copy) NSString *myStringProperty;  // In the implementation @synthesize myStringProperty = _myStringProperty; 

Should I get over my aversion to the underscore because that is the one way it should be done, is there a good reason for this style being the preferred one?

Update: With automatic property synthesis nowadays you can leave out the @synthesize and the result is the same as if you'd used

@synthesize myStringProperty = _myStringProperty; 

which clearly shows you Apple's preference. I've since learned to stop worrying and love the underscore.

like image 655
Michael Behan Avatar asked Aug 19 '10 11:08

Michael Behan


1 Answers

I always use underscores. It creates a clear distinction between local variables and instance variables. It also avoids compiler warnings in the following situation:

@interface MyClass {     NSString *name }  @property (nonatomic, copy) NSString *name;  - (id) initWithName:(NSString *) name; @end  @implementation MyClass  @synthesize name;  // The following method will result in a compiler warning // (parameter name same as ivar name) - (id) initWithName:(NSString *) name {    if (self = [super init]) {       self.name = name;    }     return self; }  @end 

EDIT:

After having to endure downvotes and reading through the comments, let me try to make my point:

Apple recommends that ivars have the same name as their property. Apple also recommends that properties start with a lowercase letter. And Apple also recommends that local variables start with a lowercase letter.

Now you have a problem, because when you read a piece of code, and you see a variable being used, you cant' tell by the naming convention if this variable is an ivar or a local variable. That sucks. The solution is to have different naming conventions for ivars and local variables. That's just plain common sense.

The way you implement this naming convention is irrelevant. If you really want, you can simply append "_WOOHAHA" to the ivar names. I don't care (but maybe others will). The thing is that people who know what they're doing have decided to go with the "underscore prefix" for ivars. IMHO, they made the right decision, even if their own company recommends something else. (the developers I'm talking about are the people writing some major Apple frameworks and the .NET Framework classes)

In the end, code quality is more important than following a stupid rule that isn't even followed by the people preaching it.


Another remark about the code you've shown: never use retain on string properties. You should use copy instead.

For more info about copy/retain on properties, see:

NSString property: copy or retain?

like image 127
Philippe Leybaert Avatar answered Sep 26 '22 06:09

Philippe Leybaert