Skip to content

Why or Why Not Objective-C?

My friend Pete recently started developing apps for the iPhone using Objective-C and loves it:

I can’t believe we all used C++ when Objective-C was sitting there. I don’t care that message passing is slower1. It’s f*cking genius.

Objective-C has been around for about as long as C++, why isn’t it more popular? The simple answer is that early on when object-oriented programming was becoming popular, C++ got into the hands of more developers than Objective-C.

The first C++ compiler called Cfront generated C code. The end result was code that just about any C compiler could compile. Shortly after, inexpensive native C++ compilers such as Zortech appeared. This was followed by vendor support from companies like Microsoft, Borland, Sun, etc.

In contrast, Objective-C was originally a product from a single company PPI (aka Stepstone). NeXT licensed the technology for NeXTstep in the late 80s but never developed into a large enough market for most developers. When NeXT was purchased by Apple, the same language and runtime were used for Mac OS X and eventually the iPhone. Certainly if this hadn’t happened Objective-C would be little more than a historical footnote, not a mainstream language.

There’s another issue when discussing Objective-C: portability. It was used by NeXTstep, now Mac OS X and nonexistent elsewhere. What happens when you want your code to run on other platforms?

When we developed Improv for NeXT we wrote the UI in Objective-C, everything else in C++. The usage of C++ was partly historical since a lot of the code started out on OS/2. But we retained this separation: source files continued to be either pure Objective-C or pure C++. There was a thin facade layer between the two. We also developed an application called Presentation Builder which produces graphs and diagrams for Improv. It was entirely written in Objective-C. When we ported Improv to Windows (and Mac for a while), we were able to retain the back-end code and rewrite the UI in C++. But we couldn’t reuse any of the Presentation Builder code since there was no Objective-C compiler for Windows at the time. We had to rewrite it from scratch. In fact, we couldn’t even salvage the Presentation Builder file format since relied on the AppKit serialization format which, as far as I know, NeXT never documented. Since Presentation Builder relied heavily on Display Postscript, we still would have had quite a bit of work to port it but we would have been able to salvage some of the code.

Apple’s current recommendations for Mac OS X developers is to write UI in Objective-C (Cocoa) and use anything you like for the rest. So what does Apple use for their own cross-platform apps? I’ve read that iTunes is more-or-less pure C++ but much of Safari is written in Objective-C, even on Windows. So Apple must have Objective-C tools (and frameworks) for Windows.

My personal take on Objective-C is that while it’s not as “pretty” as C++ it’s also much easier to learn. Objective-C is clearly an add-on to C rather than an attempt to define a new language. Objective-C syntax is derived from Smalltalk and message passing is explicit in the language. In C++ method calls use the same syntax as accessing struct members in C. But language syntax is secondary. The real beauty of developing in Objective-C isn’t just the language — it’s the AppKit/Cocoa frameworks. And a big part of that is the late-bound dynamic nature of the language, something that’s still hard to do in C++.

  1. Message passing in Objective-C isn’t always slower than C++ virtual method calls.

Posted in Programming.

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. glen says

    Objective-C gives you the full power of a true object-oriented language with exactly one syntax addition to C and, unlike C++, about a dozen additional keywords.
    Motorola VE440

Some HTML is OK

or, reply to this post via trackback.

bob congdon is Stephen Fry proof thanks to caching by WP Super Cache