9

What is the best practice approach to private methods in objective-c. That is a method that's only going to be used the class as a helper method.

In particular what's not clear to me is:

  1. Is there a need to have the method specified in the header file as private at all? i.e. why not just leave it out of the header file, and
  2. If you can leave it out of the header file, then what is the point of having private methods?
  3. Or is it the case in objective-c there is no such thing as real private methods, in which case is it better just to specify everything in the header file and no bother marking the private at all?

thanks

Greg
  • 34,042
  • 79
  • 253
  • 454
  • 1
    Just remember that there is no such thing as a 'private' method, insofar as it not being able to be called from other classes. Putting it in a Category *obfuscates* the existence of the method, but if your class implements a method it will respond to it. – Wayne Hartman Mar 14 '11 at 00:04
  • oh...ok....do most people still bother trying to mark them as private then, or just make then public and list them in the *.h file along with true public methods – Greg Mar 14 '11 at 00:17
  • If you want to present users with a clear interface, you should not list methods in the header that you do not want them to use. However, if you don't declare them in a class extension (see my answer), you will lose all the niceties of compiler-time syntax checking. Anomie's answer is fine, but it's the "C" way of doing things. Class extensions is the new "Objective C" way of accomplishing this. – DougW Mar 14 '11 at 21:58

3 Answers3

7

There is no need to specify the method in the public header file. You may want a "private" header file for use by other classes in your module, if the classes in your module are supposed to be "friends". You could even have a "protected" header file, as Apple does with UIGestureRecognizerSubclass.h for example. It's all just convention, though, nothing supported by the language itself.

A private method in Objective-C is just one that is not publicly documented; any method can still be called from anywhere, as long as the caller knows the name of it in order to create the appropriate selector. The advantage of not publicly documenting a method is that you are free to change or remove it without worrying about backwards compatibility. Leaving them out of the header file is one way of not publicly documenting them.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • 1
    No argument with your answer--it is technically correct. However, simply omitting a declaration the old C way of doing things. Using a class extension to declare your method inside the implementation file is really the new best practice for managing this in objective c 2.0. Just want to make sure people reading this pick up on that. Cheers. – DougW Mar 14 '11 at 22:03
6

What you probably want to use is called "Class Extensions". They look similar, but shouldn't be confused with Categories. This will allow you to declare private methods in your .m file, and you'll get all the nice IDE corrections and suggestions.

Here's a decent article on it
And a related SO question

Community
  • 1
  • 1
DougW
  • 28,776
  • 18
  • 79
  • 107
  • these don't seem to cover the option of just implementing a method in the "implemention"/*.m file and using it within - is there anything wrong with doing this? – Greg Mar 14 '11 at 00:16
  • @Greg: There is nothing more wrong with it than with defining a function in C without declaring it somewhere first. There are the same caveats WTF order in the source file and such, too. – Anomie Mar 14 '11 at 00:22
  • @Greg That'll work too, just realize that you have order issues. You'll still get a warning if the implementation is after a usage within the source, for example. Use an extension for just about everything internal -- makes it easy to one-stop look at the implementation inventory. – bbum Mar 14 '11 at 02:01
  • 1
    @Anomie - Heh, did you mean "WRT"? I'm not sure how WTF applies to that sentence... – DougW Mar 15 '11 at 13:13
  • Hah, yeah. I'd edit the comment, but for some reason it won't let me. – Anomie Mar 15 '11 at 14:33
2

Best practice (and is even a compiler option to check) is that ALL methods be declared one way or the other. To 'hide' helper methods from prying eyes, declare it as such in the implementation .m file, as in:

#import Client;

@interface myClass (Private)
- (void) privateMethod;
- (float) bankAccountBalanceForClient:(Client *)client;
@end

@implementation myClass
- (void) privateMethod;
{
    //foo here
}

and so on. the private methods are a Category called Private of myClass methods. This category can be declared anywhere, even in a master .h file called private methods, although that would be a maintenance nightmare.

So, using the public .h file for public methods, and the .m file to declare private methods, you have all your methods declared somewhere. I use this compiler option to ensure and force it, so that any method used is actually declared somewhere (or I get a syntax error) and thus I dont get any runtime crashes due to method not found.

PapaSmurf
  • 2,345
  • 1
  • 20
  • 10