Objective-C: when to use vs method function

I started using the Xcode refactoring capabilities (edit> refactor> extract) and noticed that Xcode suggests extracting a method or .

I read here and elsewhere about the differences between them and realized that connected to the class, but the function is not. Therefore, I am not looking for a definition, please.

Assuming no arguments are involved, when is it appropriate to use one instead of the other? I understand that if something really does not belong to the class, then it may be a function, but again, as for the definitions. I am looking for good use cases.

In my personal case, I am trying to reorganize some kind of code from AppDelegate applicationDidEnterBackground . Since this is the only place to handle events when entering the background, the only way to clear the code is to extract the routines in ... well, functions. But they will be inside AppDelegate.m, so that they would not be methods?

Mmm ..

+4
source share
4 answers

You use functions when you have, well, a function ;-) You know the definitions: a method has an implicit argument of self and can access instance variables using this; the function has no implicit arguments - all that is needed must be passed.

If you refactor a part of a larger method, that part does not access instance variables, and you do not refactor it so that the subclass can override it, and then let Xcode build you a function. When this is done, add static to it so that it is closed to the class.

You didn’t lose anything and made it clear that a piece of code is a function - it does not change the state of an object.

Of course, there is no hard line between the choice of a function and the method, its fuzzy boundary. If a piece of code, say, simply accesses one or two instance variables, but does not update them, then you can select a function - again, it is clear that the state of the object does not change. But you do not want to pass many instance variables as parameters that are simply hard to read (and inefficient).

Using functions can be good, and of course, doing it well in Objective-C is nice.

+2
source

Personally, I only use functions if and only if the following two requirements are met:

  • I use it so often in this class or in the whole project that it requires generalization.
  • It has no side effects or context dependencies (none of this mess is void *context ).

In my opinion, style C functions should only be used as a last resort or in cases where you really need functional behavior in this type of application. Event handling depends on both the application and the context, so it’s best if you leave it alone and focus on reorganizing common templates.

+8
source

A class method should usually access the instance variables associated with this class. Functions in Objective-C are not class bound and therefore do not have access to any non-public class member variables.

Consider the KNode class, which contains the _memberVar member variable.

 @interface KNode : NSObject { int _memberVar; } @end 

Any method of this class can access and modify a member variable, but any old function cannot, because it is private.

 @implementation KNode - (void)modify { _memberVar = 10; } @end 

The following function will not work

 void modify(KNode * node) { _memberVar = 10; } 
+2
source

Two small but significant benefits of features:

  • They can be internal only by labeling them static , or __attribute__((visibility("hidden"))) , which is useful for infrastructure developers.
  • They can be embedded. For example, I use this template to create a quick lazy queue:

     static inline dispatch_queue_t sharedQueue() { static dispatch_queue_t queue; static dispatch_once_t once; dispatch_once(&once, ^{ queue = dispatch_queue_create("foo", 0); }); return queue; } 
+2
source

Source: https://habr.com/ru/post/1483908/


All Articles