tags:

views:

56

answers:

3

Hey, I'm a newbie to Objective-C, but please help me out here.

What I'm seeing is that Method signatures in Objective-C that take multiple parameters seem inconsistent.

Example:

- (void) sendEmail: (NSString *) toStr
         subject:(NSString *) subjectStr
         body:(NSString *) bodyStr;

Ok, so here we have 3 "parameters" (at least that's what I'm used to calling them), but 2 of them have "external" names (subject, body) but the first one doesn't!!!!! Isn't there something wrong with that from a consistancy/style point of view??

When we call this method we do it like do it like :

[emailObj sendEmail:@"[email protected]" subject:@"testSub" body:@"this is a test"]

(hopefully I did that right, remember I'm a newbie)

So the subject and the body are clearly marked in the call, but not the "to" address? Seems really wacked to me. Why is that first parameter getting special treatment?

+5  A: 

I would say that if you're confused about what a method does, then it's probably not named as well as it could've been.

Given the selector above, I would expect the 3 parameters to be some sort of email object, a subject, and a body. Since, however, the first parameter is not an "Email" object but rather the recipient of an email, I would probably rename this method to be:

- (void) sendEmailToRecipient:(NSString *)recipient subject:(NSString *)subject body:(NSString *)body;
Dave DeLong
What if it was a method to draw a rectangle? Then it seems I would need the awkward: "- (void) drawRectangleWidth:(int) width drawRectangleHeight:(int) height", which sort of bundles the primary goal (drawing a rectangle) in with the names of the parameters.
Paul G.
Again, that's the convention Apple chose to use. Apple's point is not to shorten the expression and just to write the primary goal; the idea is that the API forces you to write the English sentences describing the action, even without explicit comments.
Yuji
+5  A: 

In most cases, methods like this are normally named so that the lack of a name on the first parameter makes sense. In this case, I would expect something like sendEmailTo.

Adam Robinson
Right, I understand, but it seems weird. I mean my goal is to send an email, the to address is just one component.
Paul G.
@Paul G.: I'm not necessarily saying that Objective C's conventions are more sensible than traditional C-style method invocation (I personally don't think they are, but that may be just because C-style is what I'm used to), but this is what they are.
Adam Robinson
+2  A: 

I guess you thought that in the method declaration

-(void) A:(NSObject*)a B:(NSObject*)b C:(NSObject*)c

A is the method name, B and C are the names of parameters.

In Objective-C, the totality A:B:C: is the method name (more technically, called the selector) and used as a unit when you call a method by name. For example,

 if([obj respondsToSelector:@selector(A:B:C:)]){
     ...
 }

checks if obj responds to A:B:C:. But [obj respondsToSelector:@selector(A:)] will be NO in this case.

So, you should really think of the totality of A:B:C: as the method name, and A is the name of the first parameter.

Note also that you can't call A:B:C: as A:C:B:, either.

Yuji
Thanks, that helps a little, but I still don't like it. I like having methods whose names are more generic and whose arguments or parameters supply some of the specifics.
Paul G.
In other words if you think of function/method calls in C, C++, Java, Perl, Javascript, C#, Python, PHP, etc the method name would typically be sendEmail and it would take 3 arguments/parameters. This also works well when you want to overload your method so that you may have one version that takes 2 args, another that takes 3. They would all have the same method name but just different number of arguments. Also, I feel that most people don't think of "A:B:C" as the method name, even though technically it may be. Most books I've seen refer to A as the method name or treat it as such.
Paul G.
Well I can't do anything for you if you don't like it. That's life in Objective-C. You need to tell Objective-C implementers in Apple. And, you should burn to ashes those books which refer to `A` as the method name. That's technically wrong and just confuses the reader. You chose a wrong book.
Yuji