Archive for December 7, 2003

Sunday, December 7, 2003

A Balanced and Fair Look at Objective-C

Jonathan Rentzsch has written a great article about what he loves and hates about Objective-C. [Update (2015-05-09): Here’s an archive.org link since the original site is down.] I’ve wanted to write this article for a long time, but now Rentzsch has saved me the time and also done a better job than I probably would have. Now I can just cover the places where I disagree or have something to add.

Love 7: Key Value Coding
I like Key Value Coding, too, but I think it’s really part of the framework, not the language. It’s made possible by Love 3: (Fairly Good) Runtime Object Model.
Hate 1: Method-call Syntax
I think the Smalltalk-style interspersing of the method name and the parameters (contra keyed parameters) is a big win. This is probably my favorite part of Objective-C. The square braces, though by no means bad once you get used to them, are kind of ugly and annoying for editing. (It helps to use a BBEdit glossary item such as #inline#[#select##insertion#], coupled with the Balance command.) Smalltalk uses parens instead of braces, and you can omit them in certain circumstances. I don’t know whether this would be possible with Objective-C, given that it has to be compatible with C’s grammar.
Hate 6: Initialization Idiom
This is kind of a restatement of Hate 2: Lightweight. I agree that assigning to self is disturbing, even if it affords a little extra power. However, I don’t think it’s fair to blast Objective-C because many of its users like to put assignments in conditions. This is a feature inherited from C, and you could say that deficiencies in Objective-C encourage the use of this idiom. But it doesn’t bother me. I’ve read about how people accidentally use = instead of ==, and how some experienced programmers like to contort the order of their equality operands to help catch this error, but I don’t think this has ever bitten me—so I vote for the extra expressiveness.
Hate 7: No Stack Objects
This is on the right track, but I’m not convinced that stack objects are the right solution.
Hate 10: Messaging nil
I’ve got mixed feelings on this one. I really like the expressiveness of being able to message nil, for instance with locks, broadcasters, and releasing. Wacky idea: perhaps there should be a way to tell the compiler (via a type modifier, perhaps) in which cases you want to be able to message nil silently.
Hate 14: id should be id*
I think this is historical. Originally, all Objective-C objects had type id, and you were supposed to treat them like primitives, not pointers. It was only later that static typing, such as NSString *foo, was added. So it might be more appropriate to ask why we have to write NSString * instead of NSString.
Indifferent 2: Memory Management
Conceptually, this doesn’t bother me, but my intuition is that the hybrid scheme is the worst of both worlds, performancewise.

And the conclusion:

ObjC is a mediocre language propped-up by a great framework.

The framework is certainly the gem. Whether the language is mediocre depends on what you compare it to. It’s no Dylan, but I generally like it better than C++ and Java.

Further reading: the discussion on Erik Barzeski’s blog.

Miscellaneous