8

Possible Duplicate:
Prefixing property names with an underscore in Objective C

In Objective-C book i am reading and in some of the code i saw, sometimes, people add underscores to the variable names.

While i realize that this is due to an established convention, I wonder:

Is there any significance whether underscore precedes or completes variable name? For example take _name, name and name_ As Objective-C programmer, what if anything, does underscore signify to you?

Community
  • 1
  • 1
James Raitsev
  • 92,517
  • 154
  • 335
  • 470

5 Answers5

19

This is largely down to personal style and defensive programming. But here is the main reason why I personally use and have seen people use the prefix.

It is to do with making your intent clearer about whether you are accessing the ivar directly or using the getter/setter.

If I have:

@property (nonatomic, retain) NSArray *people;

and:

@synthesize people = _people;

This will compile and produce the getter/setter declarations like this:

- (void)setPeople:(NSArray *)people;
- (NSArray *)people;

Now to directly access the ivar I need to use:

_people

To use the getter/setter I can use dot notation or the getter/setter like:

[self people];
// or
self.people; // which compiles to [self people];

// and
[self setPeople:newPeople];
// or
self.people = newPeople; // which compiles to [self setPeople:newPeople];

Now in my code if I accidentally just type:

people = newPeople; // will not compile

it will not compile because I am not using the getter/setter and there is no ivar called people it should be _people.

DanielG
  • 2,237
  • 1
  • 19
  • 19
Paul.s
  • 38,494
  • 5
  • 70
  • 88
5

A single leading underscore is an Apple internal coding convention, and they do it so that their symbols won't collide with yours. Unfortunately, Apple's been sloppy about publishing code examples that follow this habit, so a lot of people outside of Apple have gotten the idea that it's a good thing to do.

If you want to use a prefix on your ivar and method names, use anything but a single leading underscore.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
3

If you do use such variable names, you definitely should not use any that begin with an underscore followed by a capital letter. The C standard reserves all such identifiers for any use (§7.1.3):

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

i.e., a future revision of the C language (or a given compiler) may use any such identifier as a keyword or library function name, which may break your program. In light of this, I prefer to simply not use names prefixed by an underscore at all; you may know not to use capital letters after the underscore, but that doesn't stop the new guy from coming along and doing it.

Additionally, all identifiers that start with an underscore are reserved at file scope; another gotcha to beware of.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
1

Underscore does not really mean anything, but when you do use them, it's harder to make mistakes like this one, but I have never seen anyone using underscores at the end, only at the beginning and would advise you against adding at the end as it might be unclear to what these things mean.

I don't really add underscores to my Objective-C code, but it's mostly a matter of taste and not meaning. Just make sure you don't add underscores in the middle or end of names :)

Community
  • 1
  • 1
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • I am sure Apple used to use the underscore suffix in their core data templates but it appears they have now changed to a prefix of a double underscore – Paul.s Jul 30 '11 at 23:09
  • It's rather based on taste, there are lots of apple provided tutorials that do not use them and there isn't a common behaviour in the community. But, as I pointed out, using the underscore might make your code safer and avoid nasty bugs like the one on the question I pointed out. – Maurício Linhares Jul 30 '11 at 23:14
  • Sorry I wasn't commenting on your response I was just pointing out that I have seen it before but even Apple have ditched that in their latest templates so like you said prob not very clear. – Paul.s Jul 30 '11 at 23:16
  • No hard feelings, just complementing the answer :) – Maurício Linhares Jul 30 '11 at 23:17
  • Regarding trailing underscore, by the way, the [SQL spec explicitly promises](http://stackoverflow.com/a/19758863/642706) to never use a trailing underscore in any name or keyword. So appending your own SQL identifiers (table names, column names, etc.) with a trailing underscore eliminates any possibility of keyword collision. Also helps to clearly flag SQL language text embedded in another programming language. – Basil Bourque Aug 19 '16 at 00:24
1

underscore mean private, I personnel don't use it my self ivars, since you can always use @private and you can add ivar to the interface extension in the .m file ore even the @implementaion section of your code, it has become common recently with the advent of properties so you can use it to protected yourself form accidentally accessing an ivar in code instead of the property since the compiler can the tell you of the error, in c programming i will ofter use the leading under score to show that a function is not self contained, it to be used by another function and will also use it in C++ from ivar because C++ doesn't like a member functions and ivars to have the same name.

Nathan Day
  • 5,981
  • 2
  • 24
  • 40