OK - thanks everyone I think I understand now. Funnily enough a while after I posted I was thinking that that is why many methods are labeled 'doSomethingWith...' and I should rename the method to something like Graham suggested (resetRulerStyleWithNewStyle:newSize:newScale:textLocation)

Also I appreciate how labeled parameters would make everything much more readable - that's why I was trying to figure out how it worked and hence my question.


thanks again to all who responded,

Peter




On 22/05/2008, at 3:44 PM, Graham Cox wrote:


On 22 May 2008, at 2:48 pm, Peter Zegelin wrote:

New(ish) here.

After all the talk about the best way to learn Cocoa I thought I'd better brush up on my obj-c. I've been able to get an amazing distance without really understanding a lot as I am mainly using Cocoa as a gui around some C++ code and have been able to get a long way just by wiring things up in IB and reusing some example code. However when messaging an object with multiple parameters I have never been able to use labeled arguments - only unlabeled. However on a hunch I removed the first label and it worked.

Here is the method I was trying to call:

- (void)reset:(RulerStyle*)newStyle:(int)newSide:(int)textLoc: (double)newScale;

I have been calling it like so:

[horizontalRuler reset:style:1:1:1.0];

trying to use:

[horizontalRuler reset newStyle:style newSide:2 textLoc:1 newScale: 1.0];

obviously didn't work, but if I remove the first label:


You haven't actually given your parameters a label. Your method signature above is: reset::::


if you wanted to call it reset:newSide:textLoc:, it would have been declared like this:

- (void) reset:(RulerStyle*) newStyle newSize:(int) newSide textLoc: (int) newScale;



Now, the point of all this syntax is readability - it should be obvious what a method does in broad terms from its name, and it should read reasonably naturally. That leads to somewhat verbose method names in some cases, but the little bit of extra typing now pays off a thousand-fold when you have to maintain the code. It also leads you away from errors like this one, because the second it doesn't "read right", you might suspect you made a mistake. (For example, I can't tell if you intended your method to have three or four parameters - it's ambiguous).

So I'd respectfully suggest having a good think about how to make your method name be self-describing. Something like:

- (void) resetRulerStyleWithNewStyle:newSize:newScale:textLocation:

So when you see this used in code, you're not left guessing what all those numbers in the parameter list mean, as you are for most other languages. You *can* use unlabelled parameters, but I don't think anybody actually does, because it negates a genuine advantage of the Obj-C language.

Coming up with descriptive names that really work is hard, but it's well worth thinking about.

Also, use whitespace to separate things out - it costs nothing and makes code infinitely easier to read. One reason you might have fallen into the trap you did was because there are no spaces between the items in the parameter list; I think you're off by one somewhere half way along. I've looked at it several times and can't tell if you mean that textLoc is a separate parameter from newScale, or if newScale is just the variable used to pass textLoc. I can't tell, and presumably the compiler will have trouble as well.


hth,

G.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to