Reflections on Objective-C
alblue.bandlem.com
alblue.bandlem.com
I have a similar fondness for the Cocoa platform. I was recently asked to port some work to another platform, and considered keeping Objective-C as the language... but my love for the language runs thin once Apple's libraries are gone. The dynamic typing and categories(adding methods to arbitrary classes) are still nice though.
To illustrate how minimal Objective-C is: There isn't even a standard root class. It includes an Object class, but using it is not mandatory, and only one class used in any Cocoa app is derived from it. There also isn't a "root class" protocol. Due to the lack of a standard root class, outside the safety of Cocoa, there isn't even a standard way to ask an object what class it is or what selectors is responds to.
Yes, there is. The Objective-C run time lets you do all of this. It is in C and not Objective-C. But, NSObject is barely more than a thin wrapper for these functions, anyway.
I make OpenGL videogames but my code is written in Objective-C (long story) and so currently only supports iOS and OS X. I feel almost at home with ObjC and my codebase, I enjoy the features mentioned in this article, and it's performant enough for what I need, but I'm reaching a point where I wish my core logic code was multi platform (from PS3 to Android including iPhone). Moving to C++ would mean learning C++ and best practices (which sounds quite daunting to beginners) and rewriting the whole codebase, but the lack of practical documentation on the portability of Core Foundation looks like a big neon sign that dropping Objective-C is the only reasonable way towards a cross-platform codebase.
There are plenty of gotchas in non-GC'ed Obj-C memory management, see my previous comment in a different thread: http://news.ycombinator.com/item?id=1986799 . Also, the interaction of ref-counting with multi-threading is just awful: objects that some code uses but doesn't own must be obsessively retained-and-autoreleased in case another thread preempts and releases them. Behind the scenes, this takes and releases a lock on the object, which doesn't exactly help efficiency.
I'd rather hear about more of the things that Objective-C really gets right, instead of another "omg, I'd never heard of ref-counting before!" post. Here's a couple to start people off:
* Using named messages (instead of vtables a la C++) provides quite good introspection and run-time modification of the class hierarchy. This allows cool features like "delegates" that implement a subset of extension behaviours, and method forwarding.
* Objective-C's "informal protocols" are what we'd now call duck-typing.
Reference counting may not be built into C++ itself, but it's not super difficult to add in either.
If you ever call delete in C++, you're doing it wrong. I have a large project where I never call new or delete even once. Whereas my Objective-C has plenty of release and retain calls.
Frankly, I'm tired of the endless C++ bashing. Yes, it's a complex language. Yes, it has bad parts that you shouldn't use. But it has strengths that make it ideal for certain high-performance applications. (And before anyone brings up C, profile qsort and std::sort and then shut up).
Use the right tool for the job, and stop being religious about language choice. If you look far enough, you'll find there's something to complain about in every language: http://twitter.com/#!/ID_AA_Carmack/status/28939697453
Anyway my point was that you can find fault in just about anything. But, you'll be much better off if you found out the strengths of each language instead, and used that to your advantage. Here's a guy that has this figured out: http://www.savoysoftware.com/blog/my-iphone-is-not-a-mac-pro...
We need fewer religious language BS articles like this one, especially from a guy who seems completely clueless about C++.