<p>Being new to Objective-C (but a long term C/++) programmer I'm looking for advice/recommendations on naming conventions for variables.</p>

<p>My personal preference would be to utilize a prefix for instance variables both for clarity within functions and to prevent shadowing of function parameters. However I'm a fan of properties which rules out prefixes (unless you also prefix your property names, which doesn't work too well and looks daft). Similarly I could use the "self.variable" convention, but only if I make EVERYTHING a property.</p>

<p>So given the code below what's your preferred naming style for instance/function variables? And if you don't bother, how do you deal with shadowing on function params?</p>

<pre><code>@interface GridItem : NSObject
{
    CGRect _rect;
    ...  
}
@end

-(void) initFromRect:(CGRect)rect
{
    _rect = rect;
    ...
}
</code></pre>

<p>Cheers!</p>

[{'answer_id': 10342, 'author': 'Kristopher Johnson', 'author_id': 1175, 'author_profile': 'https://Stackoverflow.com/users/1175', 'pm_score': 1, 'selected': False, 'text': '<p>I don\'t like using underscores as prefixes for any identifiers, because C and C++ both reserve certain underscore prefixes for use by the implementation.</p>\n\n<p>I think using "self.variable" is ugly.</p>\n\n<p>In general, I use unadorned identifiers (that is, no prefixes nor suffixes) for instance variables.  If your class is so complicated that you can\'t remember the instance variables, you\'re in trouble.  So for your example, I\'d use "rect" as the name of the instance variable and "newRect" or "aRect" as the parameter name.</p>\n'}, {'answer_id': 10344, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 2, 'selected': False, 'text': "<p>Personally, I follow the Cocoa naming conventions, using camel-casing for functions and variables, and capitalized camel-casing for object names (without the leading NS of course).</p>\n\n<p>I find type prefixing makes code more opaque to anyone who didn't write it (since everyone invariably uses different prefixes), and in a modern IDE it's not really that difficult to figure out something's type.</p>\n"}, {'answer_id': 11994, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 5, 'selected': True, 'text': '<p>Most Cocoa projects use underbar as a non-<code>IBOutlet</code> instance variable prefix, and use no prefix for <code>IBOutlet</code> instance variables.</p>\n\n<p>The reason I don\'t use underbars for <code>IBOutlet</code> instance variables is that when a nib file is loaded, if you have a setter method for a connected outlet, that setter will be called.  <em>However</em> this mechanism does <em>not</em> use Key-Value Coding, so an IBOutlet whose name is prefixed with an underbar (<em>e.g.</em> <code>_myField</code>) will <em>not</em> be set unless the setter is named exactly like the outlet (<em>e.g.</em> <code>set_myField:</code>), which is non-standard and gross.</p>\n\n<p>Also, be aware that using properties like <code>self.myProp</code> is <strong>not</strong> the same as accessing instance variables.  You are <strong>sending a message</strong> when you use a property, just like if you used bracket notation like <code>[self myProp]</code>.  All properties do is give you a concise syntax for specifying both the getter and setter in a single line, and allow you to synthesize their implementation; they do not actually short-circuit the message dispatch mechanism.  If you want to access an instance variable directly but prefix it with <code>self</code> you need to treat <code>self</code> as a pointer, like <code>self-&gt;myProp</code> which really is a C-style field access.</p>\n\n<p>Finally, never use Hungarian notation when writing Cocoa code, and shy away from other prefixes like "f" and "m_" — that will mark the code as having been written by someone who doesn\'t "get it" and will cause it to be viewed by suspicion by other Cocoa developers.</p>\n\n<p>In general, follow the advice in the <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/index.html" rel="noreferrer" title="Coding Guidelines for Cocoa">Coding Guidelines for Cocoa</a> document at the <a href="http://developer.apple.com/" rel="noreferrer" title="Apple Developer Connection">Apple Developer Connection</a>, and other developers will be able to pick up and understand your code, and your code will work well with all of the Cocoa features that use runtime introspection.</p>\n\n<p>Here\'s what a window controller class might look like, using my conventions:</p>\n\n<pre><code>// EmployeeWindowController.h\n#import &lt;AppKit/NSWindowController.h&gt;\n\n@interface EmployeeWindowController : NSWindowController {\n@private\n    // model object this window is presenting\n    Employee *_employee;\n\n    // outlets connected to views in the window\n    IBOutlet NSTextField *nameField;\n    IBOutlet NSTextField *titleField;\n}\n\n- (id)initWithEmployee:(Employee *)employee;\n\n@property(readwrite, retain) Employee *employee;\n\n@end\n\n// EmployeeWindowController.m\n#import "EmployeeWindowController.h"\n\n@implementation EmployeeWindowController\n\n@synthesize employee = _employee;\n\n- (id)initWithEmployee:(Employee *)employee {\n    if (self = [super initWithWindowNibName:@"Employee"]) {\n        _employee = [employee retain];\n    }\n    return self;\n}\n\n- (void)dealloc {\n    [_employee release];\n\n    [super dealloc];\n}\n\n- (void)windowDidLoad {\n    // populates the window\'s controls, not necessary if using bindings\n    [nameField setStringValue:self.employee.name];\n    [titleField setStringValue:self.employee.title];\n}\n\n@end\n</code></pre>\n\n<p>You\'ll see that I\'m using the instance variable that references an <code>Employee</code> directly in my <code>-init</code> and <code>-dealloc</code> method, while I\'m using the property in other methods.  That\'s generally a good pattern with properties:  Only ever touch the underlying instance variable for a property in initializers, in <code>-dealloc</code>, and in the getter and setter for the property.</p>\n'}, {'answer_id': 11997, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 1, 'selected': False, 'text': '<p>Andrew: There actually are plenty of Cocoa developers who don\'t use instance variable prefixes at all.  It\'s also extremely common in the Smalltalk world (in fact, I\'d say it\'s nearly unheard-of in Smalltalk to use prefixes on instance variables).</p>\n\n<p>Prefixes on instance variables have always struck me as a C++-ism that was brought over to Java and then to C#.  Since the Objective-C world was largely parallel to the C++ world, where as the Java and C# worlds are successors to it, that would explain the "cultural" difference you might see on this between the different sets of developers.</p>\n'}, {'answer_id': 61924, 'author': 'Kendall Helmstetter Gelner', 'author_id': 6330, 'author_profile': 'https://Stackoverflow.com/users/6330', 'pm_score': 2, 'selected': False, 'text': '<p>With the introduction of properties I see no need for prefixing "_" to class instance variables.  You can set a simple rule (described in your header file) that any variables to be accessed external to the class must be accessed via the property, or by using custom methods on the class to affect values.  This to me seems much cleaner than having names with "_" stuck on the front of them.  It also properly encapsulates the values so that you can control how they are changed.</p>\n'}, {'answer_id': 64371, 'author': 'Scott Marcy', 'author_id': 8168, 'author_profile': 'https://Stackoverflow.com/users/8168', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the underbar prefix on your ivars and still use the non-underbar name for your properties. For synthesized accessors, just do this:</p>\n\n<pre><code>@synthesize foo = _foo;\n</code></pre>\n\n<p>This tells the compiler to synthesize the foo property using the_foo ivar.</p>\n\n<p>If you write your own accessors, then you just use the underbar ivar in your implementation and keep the non-underbar method name.</p>\n'}, {'answer_id': 157990, 'author': 'schwa', 'author_id': 23113, 'author_profile': 'https://Stackoverflow.com/users/23113', 'pm_score': 1, 'selected': False, 'text': '<p>My style is hybrid and really a holdover from PowerPlant days:</p>\n\n<p>THe most useful prefixes I use are "in" and "out" for function/method parameters. This helps you know what the parameters are for at a glance and really helps prevent conflicts between method parameters and instance variables (how many times have you seen the parameter "table" conflict with an instance variable of the same name). E.g.:</p>\n\n<pre><code>- (void)doSomethingWith:(id)inSomeObject error:(NSError **)outError;\n</code></pre>\n\n<p>Then I use the bare name for instance variables and property names:</p>\n\n<p>Then I use "the" as a prefix for local variables: theTable, theURL, etc. Again this helps differentiate between local and and instance variables.</p>\n\n<p>Then following PowerPlant styling I use a handful of other prefixes: k for constants, E for enums, g for globals, and s for statics.</p>\n\n<p>I\'ve been using this style for something like 12 years now.</p>\n'}, {'answer_id': 278360, 'author': 'Dave Dribin', 'author_id': 26825, 'author_profile': 'https://Stackoverflow.com/users/26825', 'pm_score': 3, 'selected': False, 'text': '<p>I follow Chris Hanson\'s advice in regards to the underscore ivar prefix, though I admit I do use underscore\'s for IBOutlets as well.  However, I\'ve recently starting moving my <code>IBOutlet</code> declarations to the <code>@property</code> line, as per <a href="https://stackoverflow.com/questions/155964/what-are-best-practices-that-you-use-when-writing-objective-c-and-cocoa#167495">@mmalc\'s suggestion</a>.  The benefit is that all my ivars now have an underscore and standard KVC setters are called (i.e. <code>setNameField:</code>).  Also, the outlet names don\'t have underscores in Interface Builder.</p>\n\n<pre><code>@interface EmployeeWindowController : NSWindowController {\n@private\n    // model object this window is presenting\n    Employee *_employee;\n\n    // outlets connected to views in the window\n    NSTextField *_nameField;\n    NSTextField *_titleField;\n}\n\n- (id)initWithEmployee:(Employee *)employee;\n\n@property(readwrite, retain) Employee *employee;\n@property(nonatomic, retain) IBOutlet NSTextField *nameField;\n@property(nonatomic, retain) IBOutlet NSTextField *titleField;\n\n@end\n</code></pre>\n'}, {'answer_id': 278387, 'author': 'Dave Dribin', 'author_id': 26825, 'author_profile': 'https://Stackoverflow.com/users/26825', 'pm_score': 1, 'selected': False, 'text': '<p>While I love using the underscore prefix for ivars, I loathe writing <code>@synthesize</code> lines because of all the duplication (it\'s not very <a href="http://en.wikipedia.org/wiki/Don&#39;t_repeat_yourself" rel="nofollow noreferrer">DRY</a>).  I created a macro to help do this and reduce code duplication.  Thus, instead of:</p>\n\n<pre><code>@synthesize employee = _employee;\n</code></pre>\n\n<p>I write this:</p>\n\n<pre><code>ddsynthesize(employee);\n</code></pre>\n\n<p>It\'s a simple macro using token pasting to add an underscore to the right hand side:</p>\n\n<pre><code>#define ddsynthesize(_X_) @synthesize _X_ = _##_X_\n</code></pre>\n\n<p>The only downside is that it will confuse Xcode\'s refactoring tool, and it won\'t get renamed, if you rename the property by refactoring.</p>\n'}, {'answer_id': 296979, 'author': 'Marc Charbonneau', 'author_id': 35136, 'author_profile': 'https://Stackoverflow.com/users/35136', 'pm_score': 1, 'selected': False, 'text': "<p>Along with what's been said here, be sure to read the Cocoa documentation on Key Value Observing compliant naming. Strictly following this pattern will help you greatly in the long run.</p>\n"}]
['https://Stackoverflow.com/questions/10314', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1043/']