Objective-C: Dynamite!
Pages: 1, 2
Using Dynamic Typing
Adding types to languages is an attempt to provide some guarantee that data objects will not be interpreted incorrectly--a float treated as an integer, a Square as a Socket, and so on. However, making a strict guarantee of this sort is undecidable; that is, no program (or finite chain of reasoning) can detect such bugs reliably. It will either reject some correct programs or accept some buggy ones, or it will go into an infinite loop and never tell us.
How then do type-checking compilers work? They have to make a Faustian bargain: they are guaranteed to terminate with an answer, but that answer is sometimes wrong. And for safety's sake, they must always err on the side of excessive safety: they will reject some programs that are type-safe and would never crash due to a type error.
Here's an example of the sort of design made simpler by dynamic typing. I often write code in which I want to send a message from one object to another when some event occurs. This is an application of the Observer design pattern. A typical C++ solution looks like this:
// An abstract class declaring the interface.
struct Observer {
void stuffHappened() = 0;
};
// Implementing the interface.
class Whatever : public Observer { ... };
// The interface of the Sender.
class Sender {
Observer* observer;
void someMethod();
};
// Implementation of Sender.
void Sender::someMethod {
// ... stuff happens ...
observer->stuffHappened();
};
Now, this is a reasonable solution for the problem, but as you see it here, it's a fait accompli. You have already:
- decided that the observer gets informed via one callback method,
- specified the callback method name, hence constrained the interface the observer has to support,
- declared that interface, and
- declared a real class that supports the interface.
The interface is a form of coupling between the Sender and Observer. Those two classes may just be in separate packages, or the Sender may be in library code from another vendor, and you are supplying the Observer. In either case, both sides have to know about the interface declaration.
This looks good as a finished design, but it's hard to arrive at such a design when you have to work this way. Interface designs change, method names fluctuate, both Sender and Observer may decide to send or observe different things. In other words, with static typing you have to know your solution before you get started.
The Objective-C way of doing this is:
// The interface of the Sender.
@interface Sender {
id observer; // Some kind of object.
-(void) someMethod;
@end
// Implementation of Sender.
@implementation Sender
-(void) someMethod {
// ... stuff happens ...
// Obj-C syntax for same method call.
[observer stuffHappened];
}
@end
What's the difference? Here you have only specified the Sender. You don't waste any time specifying the interface the Observer has to support. You have declared it as type id, which means, "some kind of class (that is, not a built-in int), but we're not being any more specific." In particular, you won't know until runtime whether the object really does support the stuffHappened message. Objective-C lets you customize the handling of (otherwise) unhandled messages: this can range all the way from nothing at all to rerouting the message to a delegated substitute object to terminating the program with a runtime error.
The Case for Dynamic Typing
So what? Doesn't this amount to so much backsliding from the hard-won lessons of data typing? Why is this not just an example of lazy programming?
There's the reasoned response, which goes like this: During development, a lot of design issues are in flux. It's a lot easier to work out how your framework is supposed to behave without having to start out in a most-factored state, with plenty of empty interface fluff. In fact, Objective-C lets you use static typing as well, so once you settle on a design, you can declare your types with no penalty. Best of both worlds.
Then there's the theoretical response: Objective-C objects are more flexible because of this tolerance for unrecognized messages. In effect, they are already implementers (in an empty sense) of an infinite number of as-yet-undesigned interfaces. You can plug them into all sorts of designs, and if they have anything to offer, any service to provide, they will provide it; otherwise they will just get out of the way.
Finally there's the radical response. Program safety is not decidable anyway. In any nontrivial program there are many bugs, among which type errors are a small subset; and an unhandled message may not even be a bug. And in any case, Objective-C provides a mechanism for intercepting and either discarding or redirecting unhandled messages. If it really is a bug, wouldn't you prefer logging an error to calling some unexpected (and definitely wrong) method, as you would do in C++ if you called a method on an object that doesn't have it? (You would have to use casting to do this, to get around the compiler's static type-checking, of course.)
This sort of design often crops up in GUI frameworks, where it's tedious (at best) for the designer to try to anticipate all the kinds of messages, and their Senders and Observers. Instead, buttons, sliders, and all the kinds of custom controls that make GUIs useful can just accept a generic Observer. For a good example of how Objective-C's dynamism can be used to create elegant program frameworks, look at Apple's Cocoa library, in particular the Application Kit and its Responder Chain.
In summary, relaxing the iron grip of static type-checking lets you write simpler, more elegant program architectures. Objective-C gives you the option of enforcing the law where you think corporate polluters will do the most harm, but looking the other way in coffeehouses where creative freedom is needed. To be sure, this is a double-edged sword; you can find more extensive discussion here.
Andrew M. Duncan started programming in FORTRAN on Control Data 6600 hardware in 1974, and a quarter century later progressed to Mac OS X.
O'Reilly & Associates recently released (December 2002) Objective-C Pocket Reference:
Sample Excerpt: Object Life Cycle, is available free online.
You can also look at the Table of Contents, the Index, and the Full Description of the book.
For more information, or to order the book, click here.
Return to the O'Reilly Network.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 13 of 13.
-
Interesting. But practical?
2003-04-29 10:47:08 anonymous2 [Reply | View]
-
Interesting. But practical?
2003-05-07 08:52:57 anonymous2 [Reply | View]
Hi,
I used to teach what you use - until I got sick and tired of it. Your "working desktop C++ application" - I won't give much for it, and you know why.
Sorry, but asking how you can try out Objective-C because all you have is WINDOWS is about as lame as it gets.
As for whether you can make useable desktop applications - I really don't know. Why don't you ask Adobe?
And finally: if I saw "how fast" in one of my classes, I would send you home. It is never "how fast" - it's "how well".
Sorry again, but it's people like you who are ruining the software industry.
You get plenty of speed with Cocoa. Just don't walk in the door with a Microsoft attitude. They might send you to the cellar and lock the door. -
Interesting. But practical?
2003-04-30 04:37:54 anonymous2 [Reply | View]
Sure, check out the 13-line 20min word processor example.
http://www.stone.com/dev/sWord/Introduction.html
-
Interesting. But practical?
2003-04-29 17:04:35 anonymous2 [Reply | View]
You should take a look on GNUstep, then. It's a LGPL implementation of the OpenStep API (like Cocoa).
http://www.gnustep.org
there is even an installer for windows, but only for the non-graphical stuff (Foundation), as the graphical part of GNUstep is still considered as alpha on Windows (but it works fine on X11).
ftp://ftp.gnustep.org/pub/gnustep/windows/Install-GNUstep-Development-Environment-0.4.1.exe
-
Intelligent and well-informed
2003-04-29 00:28:43 anonymous2 [Reply | View]
This article is the best summary of Objective-C language features I have yet read.
Use this as an executive summary when you are trying to win over to Objective-C's (and Smalltalk's) dynamic approach someone firmly rooted in their C++, and be prepared to dismiss all their implementation-level counter-arguments (dispatch performance, type safety).
Objective-C is the ideal language for empowering programmers to be productive as very small teams, or individuals.Now that machine performance makes cycle-efficiency a largely redundant consideration, it is simply the right tool for the job.
Spread the word! -
Intelligent and well-informed
2003-04-29 10:01:49 anonymous2 [Reply | View]
I agree completely, so much that I am still perplexed by the excision of Objective-C from WebObjects (once upon a time, WebObjects was a serious tool for Web application development that was taken seriously... but one day, the King declared that there would be no more Objective-C in WebObjects and cast all the skilled craftsmen out into the wilds of Java).
Sorry for the somewhat off-topic post, but Objective-C is such a beautiful thing, and this seems to be a great text from which to learn about it, that it just seems a shame that Objective-C can't be used in the place that likely brought it the most commercial success... WebObjects. -
Intelligent and well-informed
2003-05-01 19:02:33 anonymous2 [Reply | View]
I see your point and raise you. The befifits sound good, but the syntax is very strange to me, especially. [methodCall]. It is almost UML like. Why such a strange syntax?? -
[ syntax explaination ]
2003-05-18 06:44:04 a.langmead [Reply | View]
When Objective-C advocates say that the language is the addtion of Smalltalk-style objects to the C language, they mean it. In smalltalk square braces create a code block that can be passed around or called.
For example, the chunk of code below will ask object a to compare itself to b and return a True or False object repending on the result. The Boolean object will be sent the ifTrue: message with a block as the argument. The booleans that are instatiated from the 'True' class will execute the block, ones from 'False' will ignore it.
( a > b ) ifTrue:
[ Transcript show: 'a is bigger than b'. ]
-
Intelligent and well-informed
2003-05-07 08:59:27 anonymous2 [Reply | View]
Strange syntax? You find this a STRANGE syntax?
Did you find ! strange for negation?
Or for (;;) strange for an infinite loop?
Perhaps you feel most secure in the cuddly arms of Pascal?
Or what would YOU suggest, Einstein?
(Where do all these lamers come from? Yes, the US, I know that, but more specifically? Gene pool cleaner: Hurry!) -
Intelligent and well-informed
2003-05-02 07:41:55 jdhouse4 [Reply | View]
Coming from a background in Java and C++, Obj-C at first blush looked like Urdu. At a second glance, it looked like a bad dream. But after a bit of work getting over
-(id)methodName:(Class *) usingThis:(This *) andThisOneToo:(ThisToo *);
as a way of defining a method, it starts to make sense. With a sender-message looking like this,
[pokerPlayer raiseYou:bet giveCard:oldCards getCards: newCards];
(I don't play poker, so this is likely wrong, but...)
it becomes the easiest language in the world to read. Comments no longer have to explain everything, the method call itself can do allot of that.
You'll find in either AppKit or Foundation that the Class Interface files can be easy to understand. I just had to get past the odd appearance of Obj-C. For me, it has brought back some fun in app development.
I know Obj-C looks a bit strange. But if you really think about it, so did C++ or whatever language you first learned. The difference is that Obj-C will look more "normal" every day you use it until that one day comes when you are looking at C++ or whatever and you'll think that some idiot must have designed those languages to be hard to read, not easy to code.
Just my 2¢ worth.
Jim -
Intelligent and well-informed
2003-04-29 17:05:10 anonymous2 [Reply | View]
You should look on GNUstepWeb, a free implementation of WebObjects using GNUstep framework. Altough you perhaps already knows it :-)
It lacks documentation, but if you know WebObject it shouldn't be too hard :-)
http://www.gnustepweb.org -
Intelligent and well-informed
2004-03-04 16:59:51 helge5 [Reply | View]
Or as another free option, SOPE (also API compatible with WebObjects).
Actually the OpenGroupware.org application was built using that, probably one of the largest OpenSource Objective-C applications available.
http://www.opengroupware.org/en/devs/sope/index.html
http://www.opengroupware.org/






Suppose I wanted to try out Objective-C; how could I do it? I use Windows, and Objective-C is not available from Visual Studio. I suppose I could use gcc and WinMG or something, but can I actually make usable desktop applications with it?
What kind of Objective-C toolkits are available for Windows development? Are there source code repositories? If I run into problems, are there newsgroups or web sites I can get answers?
To me, the most important feature of a language is how fast I can make a complete, working application. C++ may not be the best programming language, but it is well-supported by companies and enthusiasts. There is a wealth of existing C++ code and solutions.
I can make a working desktop C++ application in minutes. Can I do that with Objective-C?