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.

2 Comments RSS · Twitter

Love 7: You're right. I kept it in because I didn't want the love count to drop to 9. It was already lopsided more than I intended.

Hate 1: I see bbum got you (grin). Thanks for the BBEdit glossary tip. I'll try it out.

Hate 10: Interesting idea. I just successfully spiked a ObjC++ template which screams if its contained pointer is dereferenced for messaging. I'll give this some more thought.

Hate 14: I've never heard this one before. Nice background info!

Indifferent 2: Judging from your comments on Erik's entry, I think we share the same viewpoint on this.

Thank you kindly for the feedback. I updated the entry on my site, pointing back to this entry.

Thanks for the comments.

Hate 1: I actually mentioned this on Ryan Wilcox's blog before I saw what bbum had written. It's always been a hot button for me (and I guess for bbum :-). BTW, you should also try this glossary item: #inline#[self #select##insertion#].

Hate 14: I got that from Brad Cox's book.

Leave a Comment