Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use "and" in Objective-C method names?

The Apple style guides for naming methods say both "Don’t use “and” to link keywords that are attributes of the receiver." and "If the method describes two separate actions, use “and” to link them." In many cases, neither of these conditions apply. For example, let's say I have a function to do some work and call a selector on a target when it is done. Should this be named

- (void)findObjectsInBackgroundWithTarget:(id)target andSelector:(SEL)selector;

or should this be named

- (void)findObjectsInBackgroundWithTarget:(id)target selector:(SEL)selector;

In that example, the selector and target are related, but not through being attributes of the receiver. A similar example is to retrieve an object with particular attributes, but the object is not the receiver of the method. Should this function be

+ (Thing *)getThingWithName:(NSString *)name andId:(NSString *)thingId;

or should this be named

+ (Thing *)getThingWithName:(NSString *)name id:(NSString *)thingId;

I know this might seem like a minor thing, but it's nice to use the style everyone expects.

like image 967
lacker Avatar asked Jul 28 '11 18:07

lacker


1 Answers

According to what Apple says, you should use "and" only if the name of your method describes the action the method does, i.e. -(void)doThisAndThat, so in both of your examples you should not use "and" in methods' names.

like image 136
Manlio Avatar answered Sep 23 '22 18:09

Manlio