Re: ObjC Method naming
Re: ObjC Method naming
- Subject: Re: ObjC Method naming
- From: "David W. Halliday" <email@hidden>
- Date: Mon, 28 May 2001 17:59:55 -0500
- Organization: Latin AmeriCom, formerly Latino Online
Scott wrote:
>
Does this mean that I could write my method calls as:
>
>
[myRect setWidth:10.0, 10.5];
>
>
Would it be acceptable without any parameter names, just commas? Or, do I
>
have to use colons?
>
>
[myRect setWidth:10.0 :10.5];
I don't see where you got this from Nat's message. As he, and others, have
pointed out, the method name was "setWidth:height:". The above have method
names of "setWidth:" and "setWidth::". These are all distinct method names.
>
>
Just trying to get as close to a c++ style calling structure as possible
>
since that's the way my brain's wired right now. Fewer keystrokes is
>
better.
Give this up. Fewer keystrokes are, less helpful to the human reader. The
idea is to make method names that are easily read by the human programmer, or,
at least, self documenting. (The compiled code is what's good for the
computer.)
By the way, if you use the language the way it's intended, the number of
keystrokes for individual method calls is not, typically, the problem, since the
measure of productivity with Objective-C and the development tools is how many
lines of code you /don't/ have to type.
>
>
Also, what does the minus sign ("-") mean in the declaration? I've seen the
>
plus sign used also. What's the difference? All it says in the docs is
>
"you could also use the plus sign..." but doesn't follow up with any meaning
>
behind it. And when I declare a method, do I HAVE to declare it with
>
parameter names or can I leave them out?
>
>
Ex.
>
- (void) setWidth:(float) w height:(float) h
The prepended minus sign ("-") indicates that this is an instance method
(one that is handled by an instance of the class), while a plus sign ("+") would
indicate that the method is a class method, handled by the class object. (By
the way, Objective-C, like SmallTalk, has true class objects, they can be passed
around and have messages passed to them just as instance objects can. Java sort
of has class objects, but, due to features of the language, and the shallowness
of the metaclass hierarchy, they are not as usable, and don't follow similar
method selection rules as instance methods---they are only slightly more useful
than C++'s class functions.)
The logical parameters ("w" and "h") are just as necessary in Objective-C as
any C based language---they're how you get at the input (or output, as the case
may be) values. (Again, the method name, here, is "setWidth:height:". Nothing
more, nothing less. It just is.)
>
>
Rock on!
>
Scott
>
>
P.S. I miss Bjarne Stroustrup!
When you think of his C++, as he has, as a multi-paradigm language, rather
than as an Object Oriented language, C++ is OK.
>
------
>
"...there's no such thing as a plain name..."
>
http://www.domainjane.com
>
>
On 5/28/01 1:26 PM, "Nat!" <email@hidden> wrote:
>
>
> Am Montag, 28. Mai 2001 um 20:41 schrieb Pedrum Mohageri:
>
>
>
>> I have a silly question about the style convention used in
>
>> Objective C
>
>> code. Take this example from page 56 of the ObjC reference.
>
>>
>
>> [myRect setWidth:10.0 height:15.0];
>
>>
>
>> Does this mean there is a method called set with two parameters width
>
>> and
>
>> height? Wouldn't that be represented like this:
>
>>
>
>> [myRect set Width:10.0 height:15.0];
>
>>
>
>> Or does it mean there a method called setWidth with two parameters, a
>
>> nameless one(default?) and height.
>
>
>
> It means that there is a method called "setWidth:height:" that accepts
>
> two parameters. All the "parameter names" are concatenated _including_
>
> the ':' to form the method name. That's important to know, when playing
>
> with selectors. Technically therefore both parameters are really
>
> nameless. Since the method declaration is also written in the
>
> non-concatenated form (just as you would call it):
>
>
>
> - (void) setWidth:(float) w height:(float) h // compiles into
>
> setWidth:height:
>
> {
>
> ...
>
> }
>
>
>
> you don't much notice it.
>
>
>
>
>
> But what really happens is that [myRect setWidth:10.0 height:15.0]
>
> translates into
>
>
>
> objc_msgSend( myRect, @selector( setWidth:height:), 10.0, 15.0);
>
>
>
>
>
> Nat!
Hope this helps.
David email@hidden