Friday, June 20, 2014

Objective-C API Fail: Ridiculously Long Method Names

Imagine the following scene:

The Java Language designers are studying other languages looking if there are any good ideas they should incorporate into the Java language. Today they are looking into Objective-C/Apple APIs. 

       Bob speaks up "what about these really verbose method names with prepositions?" 
       Everyone looks at Bob. Silence in the room.
       Bob bows his head down in shame, looks at the table.


No one in their right mind would come up with names for methods like they have in Apple's APIs. An example of a method name is instantiateViewControllerWithIdentifier. The theory is the long names make the code more readable, but the exact opposite is true - the long names make the code painful (and difficult) to read (and the method names are impossible to remember).

One of the programmers I work with has been working on Apple for four years and even he has trouble remembering the method names. Watch Apple tutorial video's and you will see even Apple's own programmers don't remember the names(!).

A better name for the example above would have been createViewController, which is meaningful, easy to read and easy to remember.

An example of their messy APIs: here is how you check the version number of the device on iOS:
   if  ([[[[UIDevice currentDevice] systemVersion] 
            componentsSeparatedByString:@"."][0] intValue] >= 7) { }

Fail. Gee Apple, how about something like:
   if (UIDevice.majorRevision == 7) { }

Everybody makes mistakes, including Apple. The choice of long and nasty method names was a real brain-fart. It's time to stop the madness.

Edit: now that the Swift language is out, perhaps rename the Objective-C language to sound more modern too. Suggested names:
  • Gotcha
  • Goodluckwiththat
  • Stackoverflowisyournewbestfriend 
  • OMG