Dynamic Features in Objective-C

Programmers that use the C and C++ languages have become used to the facility with which they can access low-level features such as memory management. The raw speed of the language is another point that makes it very hard to use other languages. On the other hand, most programming environments are nowadays organized according to object-oriented principles. It definitely makes sense to have a language capable of enable some of the features of OOP in an organized way.

With this goal in mind, Objective-C has a sane way to introduce object orientation principles to the C world. Unlike C++, which sometimes tries to extend the C model to places it shouldn’t be, Ob-jective-C just adds a new language on top of the original C. By the way, that’s why Objective-C can be used simultaneously with both with C and C++ (if you really need it).

Distinctive Features of Objective-C

First of all, unlike in C++, the object-oriented part of Objective-C is dynamically typed. You can still use types to get auto comple-tion and compilation warns when you called the wrong method, but in reality you could just use the generic id type everywhere.

The real type of an object is determined at execution through the run-time system. This way, you can send any message to any object, and it will respond based on what it knows about the message. That is why it is so easy to use duck-type on Objective-C: if you want to act as a specific object, just respond to the messages it is expecting.

The run-time system is very well thought out. If you use only standard C you get the same speed of normal C programs. If you use objects, then you will depend on the run time system as described above, but that happen only when you want or need. In Objective-C there are no delusions that you can do proper object oriented programming without a run-time system, as in C++. You will get help from the type system when needed, but if you don’t want help, the language just gets out of the way!

Because of the dynamic nature of the run time system, one can easily use designs that are simpler then in other languages. For example, objective-C libraries use a lot of delegation. The reason is that it is really easy to use. Instead of declaring a new type to inherit from another (something you required to do a lot in Java, for example), you can just supply an object that implements the required methods. The type of the supplied object is not important – any type will do. This is why model-view-controller really works in Objective-C. The controller can be the delegate for UI objects as well as model objects. There is no need for the overhead of multiple objects deriving from others just to supply a small feature.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

Post a Comment