Wednesday, June 23, 2010

A glimpse of the Objective-C langauge

The Objective-C language has had history almost as long as C++. The languages was developed in roughly the same time (Stroustrup came with the C++ idea first, but the first implementation roughly emerged at the same period). The Objective-C has long seemed to be hidden somewhere in the secluded community while its cousin has made it to a whole bunch of commercial compilers and IDEs.

Only until recently the Objective-C shows back its teeth. Now with Apple Macbooks, Macbook Pros, Mac Minis, iMacs, iPhones, iPads, iPods devices around everywhere, the Objective-C is enjoying popularity as it has never before -- to the extent that now Apple is the largest technology company in term of market cap.

The language has been with the NeXT, a company founded by Steve Jobs after leaving Apple, and the company that brought back Steve Jobs to Apple. Having its popularity at its peak, it brought about the whole tools it's been using all those time.

I have always wanted to learn this language. I remembered it's been there for very long. I saw things comp.lang.objective-c group in the Usenet. But those day when my campus' bandwidth was still very limited, and the only few NeXT workstations are out of reach, it's difficult to get access to this language and platform.

A short look at the code revealed that it is a derivative of C language that one to make sure that the message passing between objects are very clear. I heard this was an influenced by Smalltalk, which I think make senses. I often encounter a lot with people who started learning and developing C++ applications; when they talk about object's "methods", they still call them "functions". They still ask "how to call this object's function" when the correct question is "how to invoke this object's method". That's not the case when you learn the Objective-C language. "invoking a method" also called "sending message" in Object Oriented paradigm (I think it's from Message Sequence Chart, the predecessor of UML's Sequence Diagram). The Objective-C is stressing more on the latter. It must also has been influenced by Mach micro kernel design and MPI which uses message passing instead of passing a shared memory location.

The good thing about thinking in a way of "sending message" or "passing message" from one object to another is, it applies well to a fine degree of parallelism. When you are running on a lot of core (as it is now), it's good to see the objects as sending messages to each other, and not invoking methods. "sending message" across objects are safe meanwhile "invoking object's methods" must consider all the thread related stuffs.

I know that in order to become a good Cocoa (the framework for Mac OS X and iPhone apps) application developers, we'll need to learn the language well. The language is used by most of the GUI frameworks in Apple platforms. While some of the syntax is just resemble what we have in C language, the Objective-C has also departed from C language in so many ways. Some of the syntax looks very weird, especially when you are from C++ and Java background like me.
It must also has been influenced by Mach microkernel design which uses message passing instead of passing a shared memory location.


[sourcecode lang="c"]
NSString *myStr = [[NSString alloc] init];
[/sourcecode]

I don't know how weird it is for you, but this is the syntax of allocating memory for the NSString object (simply by sending "alloc" message), then sending "init" message to the newly allocated object.

1 comment: