Software Stewardship
Clever Ain't Wise

I just figured out what bothers me the most (among several other things) about Objective-C: in its current incarnation it is being to too clever with names. It heavily breaks the principle of least surprise (in this case being: “name things as I wrote them, don’t make stuff up”)

I figured this out by working with Angular-JS and its attributes on directives. Namely, it also tries to be too clever with names and translates e.g. date-format attribute in HTML to dateFormat property in JavaScript. So if you want to find (or find and replace) all the places these names are used, you will need two searches. I sure was surprised and I hope I don’t forget about it.

Objective-C with its directly/indirectly defined properties is even worse: it has two names for each read-write property plus two ways to invoke them. Consider:

@interface TooClever : NSObject

@property int threeNames;

@end

This property can be accessed with object.threeNames or with [object setThreeNames:<new value>] or with [object threeNames]. As an extra you can access it with self._threeNames from within the class code, in order to circumvent the setter and getter.

I’m very glad that Swift isn’t trying to be clever about its names:

class Wise {
  var oneName: int = 1
}

Now here there are no secretly generated or publicly declared setOneName or oneName functions - just a single oneName that is always consistently accessed with dot syntax. This is the least surprising implementation - you wrote oneName, you use oneName. Anything else is bad design. At least in the case of Objective-C, bolting properties on top of it must have been riddled with tradeoffs due to its long history. But Angular-JS has no such excuse - it seems to be clever for cleverness’ sake.


Last modified on 2014-07-07