Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of using class func vs func vs no class declaration

Tags:

swift

Ok so I have a a bunch of helper functions in my project that I originally had in a class called Animate. I was wonder what are the benefits of declaring func vc class func.

Lets use this as an example class:

class Animate{
    func moveView(...){
        ...
    }
}

So I believe if I have a class func I don't have to instantiate the class as so.

Animate.moveView(...)

And if I just declare the function with func it would be:

Animate().moveView(...)

However if I don't declare the file as a class at all as so:

func moveView(...){
    ...
}

When I call the function it is just:

moveView(...)

With no indication where the code came from and it can be just used like this anywhere in the project.

What are the pros and cons of these three ways? Is not declaring a class bad practice? Or, is there some edge case that this is very useful? For example in my situation I have no need for a class since I am just creating helper functions and not an object.

Thanks in advance for any insight on this!

like image 538
Boid Avatar asked May 12 '15 17:05

Boid


People also ask

Why are classes better than functions?

Classes provide a way to merge together some data and related operations. If you have only one operation on the data then using a function and passing the data as argument you will obtain an equivalent behaviour, with less complex code.

What is the difference between static Func and class func?

But what's the difference? The main difference is static is for static functions of structs and enums, and class for classes and protocols.

What is difference between class and function?

Classes represent object definitions. Functions are ways of manipulating objects, by taking an input and producing some sort of output.

What does class func mean in Swift?

Class functions (not instance methods) are also static functions but they are dynamically dispatched and can be overridden by subclasses unlike static functions.


1 Answers

Ok. Instance methods vs class methods vs global methods.

(The term method and function are interchangeable. Method implies a function implemented by an object, so I tend to prefer the term method to the term function.)

An instance method is a method that is performed by instances of a class. You must have an instance of that class to talk to in order to invoke an instance method.

Instance methods have access to the instance variables of the object they belong to, so the object can save state information between calls. (In a networking class you could create multiple download objects, each of which manages an individual file download of a different file from a different URL, and each might have a different delegate it notifies when it's download is complete)

Class methods are invoked by the class itself, not by an instance. This can make it simple to invoke helper functions without having to manage an object to do that work for you. Since class methods don't talk to an instance of the class, they can't preserve different state information for each object. You might have a utilities class that performs localization functions on strings for example. The localization process is self-contained. You call a class function and pass in a string and the language you want it localized to, and it hands you back a result. No need to keep state between calls. Such a call might look like

let frenchString = 
  LocalizationUtils.localizeString("English String", 
    toLanguage: "French")

Global functions do not belong to any particular class. They are global to the entire module in which they are defined. They are similar to class functions, except that they are not specific to a particular class.

like image 115
Duncan C Avatar answered Oct 03 '22 19:10

Duncan C