struggling with correct way to name this method
Long time c#/java/c++ programmer, brand spankin new to objectivce C. Read the guidelines, looked api's, but not quite there yet on the naming convention.
Example: assume I have a Cars class that has an array of every car, and you wanted methods to return a subset of the array.
I see NSArray has a m开发者_如何学JAVAethod: getObjects, but in most cases I don't see the "get". So what do you prefer?
All inputs appreciated! Spent way too much time thinking about this.
Option A) -(NSArray *) getCarsWithColor:(NSString *)color;
Option B) -(NSArray *) getCars:(NSString *)withColor;
Optoin C) -(NSArray *) carsWithColor:(NSString *)color;
OPtion D) -(NSArray *) cars:(NSString *)withColor;
Option E) none of the above, name it xxxxxxxxxxxx....
Thanks.
Objective-C methods are seldom named with get
. The getObjects:
method has get
in it only because the result is placed in a buffer in an input argument.
-(void)getObjects:(id*)aBuffer;
^^^^ ^^^^^^^
whereas your method is not filling a buffer, but return an array. Option (A) and (B) are out.
Also, the kind of argument is usually part of the selector name (stuff before :
), e.g.
-(UIView*)viewWithTag:(NSInteger)tag
^^^^^^^
// not view:(NSInteger)withTag
-(CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
// not convert:(CGPoint)point from:(UIView*)view;
so option (D) is discouraged.
A detailed guideline for naming methods can be found in Coding Guidelines for Cocoa: Naming Methods. This guideline also include other conventions which you may be interested.
Option C is the best. Never use "get" unless you're getting pointers into a C array and the arguments should only be named for the method signature that refers to them.
This way, longer methods with multiple arguments are clearer:
-(NSArray *)carsWithColor:(NSColor *)color
wheels:(NSInteger)wheels
seats:(NSInteger)seats
premiumInterior:(BOOL)premiumInterior ...
...which can be shortened to: -carsWithColor:wheels:seats:premiumInterior:...
when describing it to others.
"C" is the standard way to do it. get
is very rarely used in getters and the more verbose carsWithColor
is preferred.
In addition to what everyone else has said, I'd be curious why you're storing an NSArray of objects in the Cars class. It sounds to me like cars is an NS[Mutable]Array ivar somewhere containing instances of the car class. Then, you don't need this method at all. If you're using Core Data, then you'd do a fetch and if you're just handling the NSArray yourself, you could use a predicate to filter the array's objects. I think that is the bit that strikes me as the most un-Cocoa aspect of your question. If you do need this method, then it would be defined on the object containing the NSArray ivar like:
NSArray *cars = [NSArray arrayWithObjects:car1, car2, car3, nil];
(NSArray *)carsWithColor:(NSString *)color{
return [cars filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"color == %@", color]];
}
That code is untested, but it's how I would approach the problem. The method is really a controller type method and shouldn't be part of your model logic. Having the Cars class sounds like muddled MVC to me.
Objective-C method names do not use the get
prefix. So option C is closest to being correct, with the caveat that the aWithB
construction implies the NSArray *
that comes back will be autorelease
-d.
Oh, there are so many ways to do what you're trying to do! Usually, the "get..." notation should be avoided unless you're defining a custom getter, to avoid confusion. I vote for option C, "-(NSArray *) carsWithColor:(NSString *)color;".
精彩评论