Journal Articles

CVu Journal Vol 17, #2 - Apr 2005 + Programming Topics
Browse in : All > Journals > CVu > 172 (12)
All > Topics > Programming (877)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: An Introduction to Objective-C

Author: Administrator

Date: 03 April 2005 13:16:11 +01:00 or Sun, 03 April 2005 13:16:11 +01:00

Summary: 

Part 5 - The Philosophy Behind Objective-C

Body: 

As mentioned in Part 1, it was Cox's idea to apply the principles of Smalltalk to a lower-level language like C, thus producing a useful hybrid which combined the advantages of both. Polymorphism is implemented by late binding and is not dependent on inheritance, as it is permitted to send any message to any object. This gives the developer the maximum flexibility and enables techniques such as delegation and notification, which are very important in the construction of complex graphical user interfaces. These techniques are very difficult to implement in C++, as the compiler needs to know at compile time about all the member functions that are going to be called on an object. Objective-C programmers tend to find C++ very restrictive in this regard, and they typically say that they are having to fight against the language instead of being able to use it as a tool. In fact, I must admit to having experienced this feeling myself. Supporters of C++, on the other hand, say that having the compiler do the checking for you eliminates at an early stage a common class of errors. The fact remains, however, that certain problems are very difficult to solve in pure C++, and vendors like Trolltech (who supply the Qt application development framework) and Microsoft provide some quite elaborate mechanisms in order to get around this.

Another area where the message-passing model of Objective-C shines is distributed objects; with NeXT/Apple's PDO invoking a method belonging to an object in another thread, address space, or even a remote host differs little from invoking one in a local object..

It has been said that C++ excels at solving closed-world problems, while Objective-C's realm is an open world. Anguish et al. write:

Closed-World Applications

The engine compartment of an automobile is analogous to closed-world applications. It is desirable to know in advance every component that will be inside the engine compartment and how they will fit together. Engines are carefully designed and their design is seldom modified after they leave the factory. Any variation in the connections between engine components is probably a manufacturing error. Languages such as C++ and to a lesser extent Java provide language-level features that are well-suited to solving closed-world problems. The static strong typing used by C++ and Java enables the compiler to verify that all components fit together as planned at the cost of making variation, redesign, and modification of existing applications more difficult. Some applications require the verifiability of static strong typing and can overcome the reduction in flexibility. Some programmers are just more comfortable solving closed-world style problems and might never be satisfied with Cocoa because it is designed to solve open-world problems.

Open-World Applications

The passenger compartment of an automobile is analogous to open-world applications. Any constraints on the type or number of people and things that can be placed in the passenger compartment detract from the utility of the automobile. Some constraints are inevitable, but the designer of a passenger compartment must strive for maximum flexibility. The price of that flexibility is that the designer cannot anticipate everything that might be put in the passenger compartment. The designer must work with incomplete information. Objective-C provides language-level support for solving open-world problems. Objective-C objects can operate with anonymous objects in different applications. It is possible to send messages to objects even though the receiver might not understand the message. It is possible to add behaviors to existing compiled objects without recompiling them. The flexibility provided by Objective-C aids the development and life-cycle modification of most desktop applications, but the cost is that the compiler cannot always verify that the components fit together. In some cases, errors that might have been caught by a compiler with a different language cannot be caught until an Objective-C application is running.

Most graphical user interfaces are examples of open-world applications. Restrictions on the type and number of graphical components available reduce the utility of user interfaces. Sometimes it is necessary to create new user interface components that were not anticipated by the original designers and still be able to integrate the new components with the old components. Plug-ins and context menus are other examples of open-world applications.

It is certainly possible to create open-world applications with static strongly typed languages, but it is more difficult. It is also possible to use strong static typing with Objective-C and gain many of the benefits at the cost of flexibility. Cocoa and Objective-C emphasize flexibility at the expense of compile time verifiability. Much of the increased programmer productivity attributed to using Cocoa results from Objective-C's flexibility. (Anguish, S., op. cit. pp. 25-26)

My first introduction to C++ was something of a disappointment. To be sure, I was greatly impressed by the power of the 'template' facility to operate with arbitrary types, though it did not seem as elegant as the strong generic typing of SML and Haskell. I expected C++ to come packaged with general-purpose and GUI class es in the same way as Smalltalk, and all I got was iostreams! The position is a little better today, as the standard C++ library now incorporates the Standard Template Library, a very versatile basic set of algorithms and data structures, yet I still have the impression that people who work in C++ 'cut their own code' for the most part rather than make the maximum use of code written by others.

I hope to have convinced the reader that Objective-C is very simple in terms of both its syntax and its concepts; much like Java, it eschews 'frills' like operator overloading and multiple inheritance. It has no 'templates' either, which are perhaps the source of the best and the worst of C++. However, the APIs associated with Objective-C, for example those of Apple's Cocoa, tend to have a very steep learning curve.

As the programming language of choice for new Macintosh applications, Objective-C would seem to have a future at least as secure as that of its main platform. In the wider world of object computing, however, I perceive a trend away from low-level hybrids like Objective-C and C++ towards managed object environments like those of Java and the Microsoft Common Language Runtime on the one hand and scripting languages like Perl, Python and Ruby on the other. There are several reasons for this. First, languages in the C family allow the programmer access to raw, unchecked pointers, which, while often efficient, can also lead to application crashes and buffer overruns. The difficulty of providing reliable automatic garbage collection for these languages can also cause crashes (from deallocating unallocated memory) and memory leaks. The Cocoa frameworks use partly manual and partly automated reference counting, which is better than leaving memory management completely to the programmer, but inappropriate use of -retain and -release can still result in crashes and memory leaks, and of course, there is always the possibility of the retain count never reaching zero because of circular references; in addition, the book-keeping overhead involved with reference counting can be extremely severe where a large number of objects is inserted into and taken out of container objects. Object programming seems to me to demand automatic memory management, something which is possible in C++ and Objective-C only under controlled circumstances, like the 'safe mode' of Microsoft Visual C++ .NET. As computers become ever faster, the undoubted machine efficiency of Objective-C and C++ is perceived as less of an advantage relative to the interpreted, thus slower, but also safer, easier and more flexible, scripting languages.

Notes: 

More fields may be available via dynamicdata ..