What is the best approach when using methods / functions

First of all, I know that this is a subjective question, and maybe it will be closed. However, I have a confusion about class methods and free functions.

We all know as class methods, and in terms of code maintenance, this is the right approach. So what is the importance of using free functions, how does it improve the quality of the code.

Example:

On iOS, we create a function in a class and call it this:

 let str = self.myFunctionName() /// This is defined in same class

      

And when we use free functions, we can call like this:

let str = myFreeFunctionName() /// This defined in some other class, so it can be used in other classes.

      

which will be the more efficient of both.

+3


source to share


1 answer


It's not about efficiency here. This is a design approach. A function is a characteristic of the implementation of something. If the logic from within the function is to use some properties of the class and is specific to the class, you insert it there. If a function does some common thing and needs to be visible in the project, you leave it out of any structure. People don't usually overuse features free

.



+1


source







All Articles