Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper naming convention for a delegate method with no arguments except the delegating object?

I have a class called ABCCalendarView. It requires an NSCalendar, and gets this object from its delegate. I'm trying to work out what to call this delegate method, and I want to fit in with the best practices of the Cocoa framework.

According to the 'Naming Methods' section of Apple's 'Coding Guidelines for Cocoa', I should:

  • Start the name by identifying the class of the object that’s sending the message:

    - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;
    - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
    

    The class name omits the prefix and the first letter is in lowercase.

  • A colon is affixed to the class name (the argument is a reference to the delegating object) unless the method has only one argument, the sender.

    - (BOOL)applicationOpenUntitledFile:(NSApplication *)sender;
    

This would suggest I should call the method:

- (NSCalendar *)calendarViewUsesCalendar:(ABCCalendarView *)calendarView;

However, looking at actual examples of this situation in the Cocoa framework, I can see that the actual convention used seems to prefer putting the class name at the end of the method name in this case. For example:

// UITableViewDataSource has the method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

// UITableViewDelegate has:
- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView

// UIScrollView has:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

If I follow this convention that would suggest I should call my method:

- (NSCalendar *)calendarForCalendarView:(ABCCalendarView *)calendarView;

Why is there seemingly a discrepancy between Apple's guidelines and what they've actually done? And what convention should one follow when creating a delegate method which is basically just setting a property of the calling object?

like image 687
Craig Brown Avatar asked Oct 30 '22 04:10

Craig Brown


1 Answers

I think that rules document is slightly internally inconsistent. There are two "General Rules" which state:

If the method returns an attribute of the receiver, name the method after the attribute.

and

Make the word before the argument describe the argument.

These, along with the precedent in the SDK's names, both argue for your second formulation:

- (NSCalendar *)calendarForCalendarView:(ABCCalendarView *)calendarView;

I think there's ample evidence in the SDK names that the rule you found in the delegate section should be restated. As you point out, it says

Start the name by identifying the class of the object that’s sending the message

but it should say, in my opinion: "Send an instance of the caller as the first parameter". Which is sort of half naming rule, half design pattern. With that, those single parameter, attribute returning delegate names can be of the form returnTypeSenderType: (which is how they are in practice) without running afoul of the other parts of the doc.

like image 88
danh Avatar answered Nov 16 '22 00:11

danh