views:

67

answers:

3

I know Objective C uses 'interleaved arguments', and it is by design.

But I want to know why you think it makes life easier to merge the name of the first argument into the message name. See below:

Correct: [myRectangle setOriginX: 30.0 y: 50.0]
instead of
Wrong: [myRectangle setOrigin x: 30.0 y: 50.0]

[receiver message argument1:value1 argument2:value2...] <<< isn't this one more clear and intuitive to you guys?

+3  A: 

Trying for shortest answer:

Yes :)

Chris Kimpton
OK then, I guess I can live with it.
aXqd
+2  A: 

I think that you are trying to understand objective-c associating it with OOP programming languages like Java or C++, which I don't recommend. With time passing you will understand and like the Objc approach, it took me 6 months to really enjoy it, and now I only enjoy ObjC development :p.
Anyway the ObjC man. says the following:

The method’s actual name (insertObject:atIndex:) is a concatenation of all of the signature keywords, including colon characters. The colon characters declare the presence of a parameter. If a method has no parameters, you omit the colon after the first (and only) signature keyword. In this example, the method takes two parameters.

Source: Methods and Messaging


Edit
The ObjC way of naming its methods, I like it better because I don't have to think about a method name, but about what messages ("represented by other objects") I want to send to object A so that I get what I want from it;

NSArray *list = [object_a fromCategory:@"Literature" authorsStartingWith:@"Em"];


or

NSArray *list2 = [object_a fromCategory:@"Literature" authorsStarginWith:@"EM" fromCentury:@"16"]



Try to use natural language and translate the message to object_a, something like: "Hey object a give me from category Literature all the authors stargin with "em" and which are from cetury 16" - This is the Message - you don't need a separate method Name - all together can be seen as a method name (as the man says).
Do you get now how elegant and Clear is ObjC way to do what you used to call "method overloading"?

Andrei T. Ursan
ObjectiveC is nearer classic OOP (e.g Smalltalk) than most other languages
Mark
Actually, Objective C is much closer to "classic" OOP than languages like C++ and Java. The method naming syntax is pretty much identical to Smalltalk's method naming syntax, and the programming model is truer to Smalltalk than C++ and Java.
JeremyP
By classic oop I meant the Java and C++, because they are the industry standard, but I will edit my answer to be more clear.
Andrei T. Ursan
There is no logical reason why I would say "Give me from category Literature the authors starting with 'Em'" rather than "Give me all the authors starting with 'Em' from the category Literature" — nor is there any reason I might name the the category and the first letters of the author's name, *or* the category and first letters of the authors name and the century, but would *never* name the category and the century *without* a prefix for the author's name. For a real-world example, I can never remember the argument order for NSWindow's designated initializer. There's nothing elegant about it.
Chuck
Also, you seem to be comparing it to C++ and Java, but the example he gave is more like Python, which has proper keyword arguments. See here: http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html — It really is syntactically and semantically superior IMO
Chuck
[Order] is one thing. [Required and Optional] is another.
aXqd
+3  A: 

It's easier to implement because then selectors really are just the method's "name" as a string and the arguments can just be passed to the method in the order they were given. This allows Objective-C to be written easily as essentially a small preprocessor + set of runtime functions on top of C, which it originally was. To do it otherwise would be more complex.

It's also simpler because Objective-C's messaging syntax was derived from Smalltalk, which used the exact same way of doing selectors (though it was not a preprocessor to C), so this is zero change from the Smalltalk syntax.

You seem to be asking why Objective-C didn't take its design cues from languages that came into vogue many decades later. The answer would be: Because they weren't around yet. (I'm not sure if keyword arguments were common in Lisp by that time, but they weren't in most programming languages.)

Chuck
yea, maybe you are right. iPhone is fashion, but Objective C is certainly not :P
aXqd
hi, Chuck, I reconsidered the 1st paragraph of your comment. I don't see how hard it could be. After all, we have introduced a new syntax for message passing. I am not asking for the modern 'keyword argument' here. If we can not change the order, it's fine. All I want is just to give the 1st argument a separate name as what we can do to the 2nd, the 3rd...arguments.
aXqd
You're not giving the arguments separate names. There's only one selector. By convention, the sections name the arguments that go after, but that connection is not inherent. I think you're really asking why we can't stick a space an an arbitrary position in the first logical section of the selector. That would actually be *inconsistent*. You can't stick a space in any other arbitrary position. You can only separate the selector at the colons, where arguments go. *Could* the language have been designed to include this inconsistency? Sure. But it wasn't, and it makes little difference.
Chuck
Thanks, I finally got it. Yes, in the perspective of selector, adding a separate message name for it would actually be inconsistent. There's only one selector, hence we should regard both the message name and all the arguments as a whole to be the selector, e.g. [receiver] - [myRectangle], [selector] - [setOriginX:Y]. That make sense. :)
aXqd