Paul
Paul

Reputation: 6176

Use category for private method in implementation file?

I don't really understand the point to write a "category" in the appDelegate for example, just to add a method, that seems to be "private". Why would we do that? i don't really see why it is important to "hide" or "store" this private method in the implementation file, like in this example :

#import "LetsMakeAThreadAppDelegate.h"

@interface  LetsMakeAThreadAppDelegate (Static)
    - (void)backgroundThinking;
@end

@implementation LetsMakeAThreadAppDelegate
...

Do you have any advice? Thanks
Paul

Upvotes: 1

Views: 664

Answers (2)

Toastor
Toastor

Reputation: 8990

There's a number of reasons why you would hide methods, but it all (imho) boils down to two words: "programming style".

  • While you still can send an object a message calling a hidden method, you'll receive a warning that the object might not understand it. This may be used as a reminder that you're calling a method which was never intended to be called from another object.

  • Hiding "workhorse" methods also adds to the tidiness of the list of code completion suggestions, making it easier to find the right one fast.

  • If you're working on a team or intend to share the code you're writing, hiding all methods but those needed to interface with your class may improve working conditions significantly. While you may not want to hide the actual code, it helps preventing teammates from using methods, whose results are undefined if used out of context. This way, teammates only need to understand the interface of your class, not it's internals to be able to use it.

  • Last but not least, I found that taking the time to hide methods when appropriate also helps (me) reducing the amount of spaghetti code I write, since it forces me to reconsider - what does this method actually do? Would the same functionality be useful elsewhere, should I wrap some of its code into a seperate utility class so it's easier to reuse it?

Of course, if you're extremely disciplined and don't tend to experimenting / coding without a serious planning stage, none of this may be of relevance to you.

Upvotes: 3

EmptyStack
EmptyStack

Reputation: 51374

Categories (in the context of private methods):

Objective C doesn't provide any predefined way to declare private methods. So categories are used to declare them.

If you add a category to a class inside an implementation (.m) file of a class (not necessarily the same class), the methods you declare inside the category wont be visible to other classes because they are declared inside an implementation file, not the header (.h) file. So, those methods will become private to the class where they are declared.

But, If the category is added inside a .h file, the methods declared inside the category will be accessible by other classes by simply importing the header .h file. Anyway importing the file doesn't do any magic in this case. I just suppresses the "may not respond to selector" warnings.

Upvotes: 3

Related Questions