Question

I'm a C#/Java developer learning Objective C. At first I assumed "messages" were just a different name for method calls, so:

[person jumpInTheAir];

would just be the Objective C syntax for writing

person.jumpInTheAir();

But now I've read here and here and various other places that the concepts are actually not the same and can have different behaviours/advantages. However I'm still uncertain why the language designers would choose a messaging system over a more direct method calling system as in C# and Java.

What advantages do messages bring to the Objective C programming language?

Was it helpful?

Solution

At first I assumed "messages" were just a different name for method calls,

Yes and no. "Messages" are the Smalltalk and Objective-C terminology for method calls. The thing is, it's not only the terminology that differs, but the actual implementation too. There are 8 different possible combinations for matching the terminology, the syntax and the implementation, like this:

+-------------------+----------------------+--------------------+
|    terminology    |        syntax        |   implementation   |
+-------------------+----------------------+--------------------+
|  "method call"    | Simula (o.method())  |  static binding    | non-virtual C++ methods
+-------------------+----------------------+--------------------+
|  "method call"    | Simula               |  dynamic binding   |
+-------------------+----------------------+--------------------+
|  "method call"    | Smalltalk ([o meth]) |  static binding    |
+-------------------+----------------------+--------------------+
|  "method call"    | Smalltalk            |  dynamic binding   |
+-------------------+----------------------+--------------------+
| "message passing" | Simula               |  static binding    |
+-------------------+----------------------+--------------------+
| "message passing" | Simula               |  dynamic binding   |
+-------------------+----------------------+--------------------+
| "message passing" | Smalltalk            |  static binding    |
+-------------------+----------------------+--------------------+
| "message passing" | Smalltalk            |  dynamic binding   | Objective-C
+-------------------+----------------------+--------------------+

The combination the language designer choses is just a matter of taste.

I'm still uncertain why the language designers would choose a messaging system over a more direct method calling system

Because it has some advantages, such as runtime interposition and introspection -- one can query and modify the behavior of classes, methods and objects at runtime. In the implementation of Objective-C, this have been done in a way such that it is very cheap, it has almost no overhead.

OTHER TIPS

H2CO3 pointed out in his answer that there are three things I might be referring to when I say "messages": Terminology, Syntax and Implementation.

Terminology and Syntax are down to personal taste, but implementation (static/dynamic binding) has some real consequences.

Pros:

  • Dynamic binding can be used to add new methods to an existing class without subclassing. This seems to be a possible replacement for those used to C# Extension Methods.
  • It lets you inspect the methods available and change their implementation at runtime. See Reflection and Method Swizzling in Objective C.

Cons:

  • It can be argued that the extra layer adds complexity to both the syntax and usage.
  • Dynamic binding adds some overhead over static typing, but this is rarely the bottleneck and it seems there are ways of getting around the overhead if it is really critical.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top