Code Guidelines
Code Guidelines
- Subject: Code Guidelines
- From: Christoffer Lerno <email@hidden>
- Date: Fri, 28 May 2004 12:16:58 +0800
Short version:
What is the general consensus of how to name instance variables so that
they can be separated from local variables and parameters?
Longer version:
I'm trying to figure out some code guidelines. Basically I'm sticking
with the Apple ones, but there are places where you're on your own.
For Java, I had this pretty useful code guideline:
private Object mSomeObject;
protected Object mSomeObject;
private static Object sSomeObject;
public Object pSomeObject; // only for my mock objects during unit
testing
public static final String SOME_CONSTANT="blah";
private static final String OTHER_STRING_CONSTANT="zip";
public SomeMethod(int someparameter,String string);
The advantage of that was code like this:
public setName(String name)
{
mName=name;
}
And it was very clear what was what.
Anyway, looking at examples, it seems that in general, Obj-c code would
tend to look like this:
- (void)setName:(NSString *)newName
{
name=newName;
}
The problem is that it's nowhere clear which one is the parameter. If
you throw in some temp variables, things become even less clear.
I mean, in java I would never ever by accident have code like this:
@interface
{
SomeObject *object;
}
@end
@implementation
- (void)doSomething:(SomeObject *)anObject
{
Object object=anObject;
}
@end
Because the corresponding code would look like this with my java code
guidelines:
SomeObject mObject;
public void doSomething(SomeObject object)
{
Object mObject=object;
}
To be able to be "wrong" in the same manner, but since local variables
and parameters never use prefixes, I would immediately spot that.
Sure, the compiler catches this problem for obj-c, but I don't want it
to happen at all, because one of those days I'm gonna make an error
that the compiler doesn't spot.
Anyway, why don't I use my java guidelines then? Because none of the
code I see anywhere is using one-letter prefixes like my java code and
it makes my code look like it doesn't mesh so well with the other stuff
out there.
What are your guidelines and why?
_______________________________________________
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.