views:

94

answers:

3

Hey guys, I had a question about naming conventions. I noticed in alot of cocoa classes people have made, and in apples core they name some of there variables like __name is there a reason for having the __ is this something I should be doing in my own classes? If so where and when should I use it?

+8  A: 

That's called uglification. The point is that you never use it, so no variable name or #define you create could ever interfere with Apple's code.

Ironically, many people create header guards with such names, because they see the system headers do it.

From C99 7.1.3 "Reserved identifiers":

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

(They mean reserved for the system library.)

Note: I'm not sure of the exact relationship between C99 and Apple ObjC, but you might as well have naming conventions that work across the entire C language family. In particular ObjC++ would require valid C++ names, which have the additional requirement of no double underscores anywhere.

Potatoswatter
I'm not sure I understand, if I am writing my own classes should I uglify some variables I don't intend on subclasses ever accessing? Or should this be an apple-code-only thing?
Alex Gosselin
So basically its just to avoid variable names running into eachother?
Justin Meiners
@Alex: You can choose your own uglification syntax, but initial underscore-capital and underscore-underscore (and C++ also reserves double underscore anywhere in the name) are reserved for the system. This applies to all platforms; it's a language feature.
Potatoswatter
+4  A: 

In Cocoa, it's a convention to indicate the something is private and shouldn't be used externally. However, it's unofficial convention, particularly in light of wording like this in the documentation:

Method names beginning with “_”, a single underscore character, are reserved for use by Apple.

However, that recommendation specifically applies to methods, not variables. So if you'd like to prefix your variables with underscores, go right ahead. That being said, if you're using the underscore prefix to indicate the private nature of some data, perhaps you shouldn't be exposing it in the first place...

Dave DeLong
+2  A: 

Underscores get in the way of readability. Also with LLVM in place of GCC, I am getting rid of header side ivars and using header side properties. Be sure to make your properties non atomic unless you really want reads and writes synchronized for thread safety. unless you specify non atomic, it will default to atomic - which will deprive you of some performance.

also by convention, never start accessors with get. setters Should start with set but no getters with get. Read up on KVO and KVC for more about the conventions and what they are good for.

I do however like underscores in Enumeration naming list. Here the underscores help me pick out the suffix in 5 or more lines that all start with the same stem. Like typedef NSInteger COMPASS_DIRECTION; enum { COMPASS_DIRECTION_NORTH, COMPASS_DIRECTION_EAST, COMPASS_DIRECTION_SOUTH, COMPASS_DIRECTION_WEST, };

Alan Morford