Friday, March 19, 2010

Apple and Naming Conventions

For those not familiar with Cocoa (Mac OS, iPhone) development, Apple has strict naming schemes for their libraries. They actually have an entire document dedicated to this (seen here). Methods that return a boolean are "shouldPerformAction", actions which are about to happen will look like "willDoSomething", actions that just completed will look like "didDoSomething", etc. A lot of Cocoa criticism comes from the fact that this causes a lot of the method / variable names to be extremely verbose. "Can row x in a table be selected?" becomes

(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

"Trim this string" becomes:
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]

I initially disliked this when first starting iPhone dev, however after a number of months it's really grown on me. Yeah some of the lengthy stuff is annoying, but it really makes a lot of the code REALLY CLEAR on what's happening and that is extremely valuable.

Anyways, sometimes this naming scheme breaks down and that's what I'm here to complain about. When the user taps a row on a table (like in the email or iPod app) the following method is called:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

There's also others that follow this:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

Some cells have a little blue circle with a > inside of it, this is called a UITableViewCellAccessoryTypeDetailDisclosureIndicator (yeah). When tapped, the view slides over to a more detailed view of the data. For example you tap a row in the Recent Calls list and it calls the number back, tap the blue circle and it brings up contact information for that person. So what method is called when this is tapped?
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

Huh? Not at all what you'd expect. I figured I'd be looking for "didTapAccessoryButtonForRowAtIndexPath". There's no "did" in this. Instead of "...RowAtIndexPath..." we have "...RowWithIndexPath...". Why is this so different? I know this sounds like a tiny nitpicky thing, but Apple has put literally thousands of man hours into standardizing this shit. It makes no sense that this single method name is so different from the others.

Now, it didn't take me more than a few seconds to find the method name I was looking for, so I'm not complaining about it destroying my productivity or anything, but it just doesn't make any sense for this to be so deviant.


Hilton said...

Objective-C is definitely an acquired taste. And sometimes you have to spit the seeds out.

Samuel Reid Hughes said...

Hey, at least on Chrome on the mac, your code examples are getting truncated.