Re: Code Guidelines
Re: Code Guidelines
- Subject: Re: Code Guidelines
- From: Joakim Danielson <email@hidden>
- Date: Fri, 28 May 2004 13:17:40 +0200
On 2004-05-28, at 06.16, Christoffer Lerno wrote:
Short version:
What is the general consensus of how to name instance variables so
that they can be separated from local variables and parameters?
I want my code to be as clean as possible so I don't use a prefix for
my instance variables (underscore is an exception but since it is
reserved by Apple I can't use that). For parameters or local variables
I try to use "a" or "an" as a prefix.
For me it's important to find a way of naming classes, methods etc that
is pretty similiar between Java and Objective-C so I don't start mixing
different styles depending of what language I've been using mostly the
latest time.
I agree that the get and set methods for instance variables are not
very clear but they are usually very short methods and with a standard
layout. Like this:
- (NSString *)name {
return [[name retain] autorelease];
}
- (void)setName:(NSString *)aName {
if (name != aName) {
[aName retain];
[name release];
name = aName;
}
}
One terrible exception is of course if you have a lazy initialization
which is really strange to read
- (NSString *)name {
if (!name) { //This is confusing!
[self setName:[self loadNameSomehow];
}
return [[name retain] autorelease];
}
If you always use the accessor methods, which you should, in your other
methods I think it's pretty easy to see the difference between a local
variable and instance parameters.
aName = [self name];
...
[self setName: aName];
Joakim
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.