hzxu
hzxu

Reputation: 5823

Delegate method naming convention if class name is long?

I am having a problem with method naming. I want to follow the method naming format as seen in UITableViewDataSource:

- (int)numberOfSectionsInTableView:(UITableView *)tableView;

but my class name is much longer than 'UITableView' -- it's XYPagedContentScrollView where XY is the class prefix for the project. I have a delegate protocol:

@protocol XYPagedContentScrollViewDelegate <NSObject>
- (CGFloat)defaultPageHeightForPagedContentScrollView:(XYPagedContentScrollView *)pagedContentScrollView;
@end

and this method name looks ridiculously long. I know it is not a strict rule, but I just wonder is there any way to deal with the long name and still conform to Apple's naming convention? Something like:

@protocol XYPagedContentScrollViewDelegate <NSObject>
- (CGFloat)defaultPageHeightForPagedContentScrollView:(XYPagedContentScrollView *)pagedContentSV;
@end

or even:

@protocol XYPagedContentScrollViewDelegate <NSObject>
- (CGFloat)defaultPageHeightForPCScrollView:(XYPagedContentScrollView *)pagedContentSV;
@end

Can anyone help?

Upvotes: 0

Views: 1478

Answers (2)

Jesse Rusak
Jesse Rusak

Reputation: 57168

Personally, I would go with the long form: that's what autocomplete is for. That being said, if you do want to shorten it, I would drop adjectives. Something like:

@protocol XYPagedContentScrollViewDelegate <NSObject>
- (CGFloat)defaultPageHeightForScrollView:(XYPagedContentScrollView *)pagedContentSV;
@end

I wouldn't worry about name conflicts with delegate methods because if an object is ever the delegate of two different object with the same delegate methods, then it will use the passed (i.e. calling) object to distinguish them. (For example, If you're the delegate of two scroll views, you just test the passed view to find which scroll view it's talking about.)

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

The length of a method is not limited by the language in any way, so feel free to make it as long as you want.

It may look bad at first, but the more verbose it is, the less chance you have of conflicts later down the line.

My rule is, if it looks like it could conflict with another method (especially if it's a delegate method), it needs a longer name.

Now, with that said, don't make names ridiculously long,so that they are 100s of characters in length, like this:

-(void) fooBarViewController:(FooBarViewController *) controller didTapRightMarginAtPoint:(CGPoint point) ofPage:(NSUInteger) page lengthHeld:(NSTimeInterval) tapLength fingers:(NSUInteger) fingerCount;

Instead, consider wrapping it in a dictionary of the event details:

-(void fooBarViewController:(FooBarViewController *) controller didTapMargin:(NSDictionary *) eventDetails; 

/* eventDetails should contain the following information:
    which margin?
    touch location
    page index
    touch length
    finger count
 */

This simplifies the method signature, and allows the receiver to only pull out the variables they need.

Upvotes: 1

Related Questions