declare all implementation functions in header file?

Discussion in 'Mac Programming' started by Darkroom, Jun 25, 2008.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    ok makes sense...

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

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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...
     
  5. macrumors newbie

    Joined:
    Apr 18, 2008
    #5
    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.
     
  6. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #6
    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.
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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.
     
  8. macrumors 65816

    devman

    Joined:
    Apr 19, 2004
    Location:
    AU
    #8
    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.

    do you mean selector?
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    Yes that could be my nagging thought :)
     

Share This Page