Re: difference between a method and function?
Re: difference between a method and function?
- Subject: Re: difference between a method and function?
- From: "Alastair J.Houghton" <email@hidden>
- Date: Sun, 24 Aug 2003 22:52:26 +0100
On Sunday, August 24, 2003, at 09:37 pm, Ben Dougall wrote:
this might be a silly question but... i'm writing a bit of code that
is contained pretty much within one object, apart from using the
pre-written cocoa objects, and i'm wondering, from that point of view
(from within one object), what's the difference between a method and a
function? what are the advantages / disadvantages of either if any?
Pros & Cons:
1. Methods
+ Named arguments make calls very easy to read.
+ Can override methods in derived classes.
+ Methods get direct access to object member variables (no need for
explicit use of pointers).
+ Many Cocoa objects implement target/action informal protocols, which
can only be used with methods.
- Methods are always "visible" in some sense... in particular it is
possible to accidentally override a method if you didn't know it
existed, even if that method is notionally private to the object's
implementation. This can sometimes be a plus rather than a minus.
- Method invocation takes slightly longer than an ordinary function
call (unless you use IMPs), although it is competitive with calls into
shared libraries.
2. Functions
+ Work in C as well as Objective C.
+ Can limit the scope to an implementation file (and could therefore be
considered as an alternative to "private" [i.e. funny-named] methods as
an implementation technique).
+ Calls are slightly faster (unless you use an IMP).
+ Can be inlined (methods can't be inlined because that would prevent
you from overriding them). This can be a minus if you're trying to
debug your program though :-)
- Don't have access to object member variables (unless written within
the bounds of an @implementation clause, in which case they still don't
have direct access [they need a pointer to the object]).
- Don't have named arguments; poor choice of arguments is more likely
to lead to very confusing function calls (for example, MFC on Windows
has a function "UpdateData", which takes a boolean parameter that
specifies whether data flows from member variables to the dialog or the
other way around... but what does
MyDialog::UpdateData(FALSE);
do?!)
===
There are probably many more and I am sure others will point them out;
those are just the ones that spring to mind at the moment. Generally
speaking if you're writing OO code you will tend to prefer methods and
objects over having lots of functions, although it takes a certain
amount of skill with OOA/OOD to do this properly for any large-scale
project (probably more than I would claim to have).
Kind regards,
Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.