Struggling with the right way to call this method

For a long time, a C # / java / C ++ programmer, the spankin brand, new for objectification C. Read the recommendations, looked at the api, but not quite there by naming convention.

Example: suppose I have a Cars class that has an array of each car, and you want the methods to return a subset of the array.

I see that NSArray has a getObjects method, but in most cases I don't see "get". So what do you prefer?

All inputs are appreciated! Too much time spent thinking about it.

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, do not name it xxxxxxxxxxxx ....

Thanks.

+4
source share
6 answers

Objective-C methods are rarely called get . The getObjects: method has get in it only because the result is buffered in the input argument.

 -(void)getObjects:(id*)aBuffer; ^^^^ ^^^^^^^ 

whereas your method does not fill the buffer, but returns an array. Options (A) and (B) are not available.

In addition, the type of the argument is usually part of the selector name (material before : , for example.

 -(UIView*)viewWithTag:(NSInteger)tag ^^^^^^^ // not view:(NSInteger)withTag -(CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view // not convert:(CGPoint)point from:(UIView*)view; 

therefore, option (D) is discouraged.

A detailed guide to naming techniques can be found in Cocoa Coding Guides: Naming Techniques . This guide also includes other agreements that may interest you.

+5
source

Option C is the best. Never use “get” unless you get pointers to a C array, and the arguments should only be specified for the method signature that applies to them.

Thus, longer methods with several 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.

+1
source

"C" is the standard way to do this. get very rarely used in getters, and more preferably carsWithColor .

+1
source

In addition to what everyone else said, I would be curious why you store NSArray objects in the Cars class. It seems to me that cars are NS [Mutable] Array ivar somewhere with examples of car class. Then you do not need this method at all. If you use Core Data, then you would make a selection, and if you just process the NSArray yourself, you can use the predicate to filter the objects in the array. I think this is what strikes me as the most informal aspect of your question. If you need this method, then it will be defined on the object containing ivar NSArray, for example:

 NSArray *cars = [NSArray arrayWithObjects:car1, car2, car3, nil]; (NSArray *)carsWithColor:(NSString *)color{ return [cars filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"color == %@", color]]; } 

This code has not been verified, but I kind of approached this problem. A method is really a controller type method and should not be part of your model logic. Having the Cars class sounds like a confusing MVC to me.

+1
source

Objective-C method names do not use the get prefix. Thus, option C is closest to the correct one, with the caveat that the aWithB construct implies that NSArray * will be autorelease will be autorelease -d.

0
source

Oh, there are so many ways to do what you are trying to do! As a general rule, the notion of "get ..." should be avoided unless you define a custom getter to avoid confusion. I vote for option C, "- (NSArray *) carsWithColor: (NSString *) color;".

0
source

All Articles