Gourav Joshi Gourav Joshi - 3 years ago 58
iOS Question

What is the best approach when using methods

First for all I know this a subjective question and may be it will be closed. However I have a confusion in class methods and Free functions.

As class methods we all are aware and as per maintain code quality it is right approach. So what is the importance of using free functions how it improves the code quality.

For Example:

In iOS we are create a function in a class and call it like:

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

And when we are 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 more efficient from both.

Answer Source

It's not about efficiency here. It's a design approach. A function represents an implementation characteristic to something. If the logic from within a function is meant to use some of the class properties and is a specific to a class, you embed it there. If a function performs some totally general stuff and is meant to be visible across the project you leave it outside any structure. People don't usually abuse with free functions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download