Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 method: 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.

like image 947
yellowdog Avatar asked Feb 25 '23 20:02

yellowdog


1 Answers

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.

like image 149
kennytm Avatar answered Mar 07 '23 05:03

kennytm