Thursday, February 05, 2009

I'm trying to read some code in Objective-C. This is hard, but it's solidifying my abstract understanding of programming languages. I'm not that hardcore a programmer, but I guess I've picked up a few things over the years (gawd that makes me sound old).

One of the neat things about Objective-C is that using a class or instance method involves sending a message. C++, in contrast, calls those methods. A C++ object has a fixed number of methods that can knowably be called. An Objective-C object might be able to handle arbitrary messages. This makes some things harder and somethings easier: polymorphism is easier; finding cases of using the wrong type of object or having a null object are harder to detect (must be done at runtime, not compile time).

The thing is, this is very familiar to me because this is how wire protocols work. In fact Objective-C has "protocols" which are interfaces, or a set of messages, that an object claims to be able to handle, so the terminology overlaps quite a bit. Anyway, in a wire protocol the client sends a message, and because anything can happen to that message, the client has to be able to handle a large number of outcomes. Polymorphism? You bet; a server that appears to be a HTTP server (implements the HTTP protocol) can also be a WebDAV server, a CalDAV server and an FTP server.

Designing protocols can be hard for people who think in terms of fixed interfaces à la C++. RPC-style protocols embody this thinking, making Remote Procedure Calls and expecting predictable, limited results. It makes more sense to me now, that RPC-style protocols are so brittle: designers and implementors are acting as if there's compile-time checking of the remote interface, whereas since the remote interface is on somebody else's computer that may have been upgraded or may just have a different implementation, of course there's no compile-time checking.

3 comments:

Anonymous said...

> One of the neat things about Objective-C is that
> using a class or instance method involves sending
> a message. C++, in contrast, calls those methods.

Methods are also called methods in ObjC and they differ little to C++ methods. Methods are just code implementation fragments.

The difference is how the methods are invoked. In ObjC method invocations are (usually) done by sending messages. While in C++ you directly dereference a method and call it. (which you can also do in ObjC)

Thats why I call C++ _class_ oriented and ObjC _object_ oriented. Due to the messaging layer in ObjC (and Smalltalk) the methods are decoupled from the type and the object itself can decide how to dispatch the message.
In C++ its hardcoded by the type (class).

Anyways, main point: methods are methods. Messages are another abstraction on top of methods. All major C++ frameworks (Qt) have messages, except it comes w/o syntactic sugar.

Lisa said...

I agree with what you said (anonymous), so I guess I explained myself poorly. The syntax for calling a method is arbitrary and easily learned, but what happens under the covers is subtle.

Anonymous said...

I just recently took my first look at ObjC code and found it pretty impenetrable, which surprised me (I was thinking about porting the ChandlerQE app to the Android platform). A while back I counted 21 languages (http://www.heikkitoivonen.net/blog/2008/08/06/list-of-programming-languages-i-have-used/) I have had to write at least one line in, so I was not really expecting ObjC to be that different/difficult.

Blog Archive

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.