iPhone ivar naming convention [duplicate]

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.

Leave a Comment