PDA

View Full Version : declare all implementation functions in header file?




Darkroom
Jun 25, 2008, 08:16 AM
is it generally good practice to declare all functions of the implementation file in the header file? it seems i can get away with not declaring some -(void) or -(id), etc. functions, so i'm assuming it's not mandatory to do so, but should i do this regardless of the app already working properly?



robbieduncan
Jun 25, 2008, 08:20 AM
You only need to declare methods that are:

a) Not inherited from the super class
b) Intended for use by other objects

So init and dealloc do not need to be delcared: they are declared in NSObject.

If you have purely internal methods you don't need to declare them. A convenient way to declare them but not clutter your main .h file is to declare them in a category called Private. This will prevent compiler errors if you use them before the method declaration as you can import the Private category header in your .m file.

Darkroom
Jun 25, 2008, 09:22 AM
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

robbieduncan
Jun 25, 2008, 10:31 AM
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

Not absolutely sure either is 100% correct in Obj-C.

Methods are an Object-Oriented term that means, roughly, a function associated with a class. Methods implement the functionality of a class along with it's encapsulated data.

Functions are normally procedural. I.e. they are not part of a class but stand alone code. In Obj-C we can use functions outside of classes (for example NSLog).

So we should not be calling class functionality "functions", rather class or instance methods (depending if they are + or -). I have a nagging thought that method is a Java term and they are called something else (not function) in Obj-C though...

MonkeyCookie
Jun 25, 2008, 10:37 AM
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

In Objective-C, C#, and Java, functions that are part of a class are usually called methods. C++ uses the term "member functions". Functions that aren't part of a class (possible in C++ and Objective-C) are generally referred to as functions. In C, from which Objective-C derives, there are no classes, so they are all called functions. In some other languages, they are referred to as procedures.

It's not very important whether you call them methods or functions: everyone will know what you're talking about.

ChrisA
Jun 25, 2008, 10:45 AM
ok makes sense...

i've called them functions, you've called the methods... can they be called either?

Some of us learned all this before the term "method" was invented and will continue to say "function". Also in C++ methods are implemented as functions. Same for "Memory" and I sometimes slip and say "core" because that was the technology back when many of us were in school. Even today dumps are called "core files". I would not worry about what is "correct" as long as the idea gets across.

robbieduncan
Jun 25, 2008, 10:49 AM
The problem with calling methods functions in Obj-C is that you can still define and use pure C functions. If people use the correct terminology it's easy to tell what they are talking about. If they don't it can get confusing.

Note that there can be very good reasons to use C functions: calling one is much faster than passing a message to an object in Obj-C.

devman
Jun 25, 2008, 10:50 AM
I have a nagging thought that method is a Java term

method is a term that long predates Java. The data of a class is called attributes and the behavior is called methods. These two names were de facto standards for a long time. Then the UML came along and decided to call methods operations.

and they are called something else (not function) in Obj-C though...

do you mean selector?

robbieduncan
Jun 25, 2008, 10:52 AM
do you mean selector?

Yes that could be my nagging thought :)