Benefits of using class func vs func vs no class declaration

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.


For me, I use static or class methods to control class level properties or if I have to return customised instances of that particular class or struct. Like for example consider I have below struct.

struct Person {
  let firstName: String
  let lastName: String
}

Now if I am writing some test cases, where I need Person's instance initialized with a particular name John in many of my test classes, I can create a helper static method.

extension Person {
    static func john() -> Person {
        return Person(firstName: "John", lastName: "Appleseed")
    }
}

let john = Person.john() // I could also create a static property instead, but it's a matter of personal choice and situation.

In above case, I could have made john as global function as well but for me, it will be very vague and not readable.

Another place I can think of where I prefer static method is returning count of cases for an enum.

enum Mood {
    case happy
    case angry
    case lazy
    case high

    static func count() -> Int {
        return 4
    }
}

There are places, where I use global functions. I use global functions for logging.

func log(screenEvent name: String)  {
    let tracker = GAI.sharedInstance().defaultTracker
    tracker.set(kGAIScreenName, value: screenName)
    let builder = GAIDictionaryBuilder.createScreenView()
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Internally, the method is using a sharedInstance, creating a global method makes it easily accessible everywhere in the project just like a print function which logs output in console, but this is logging in some custom service.

Some other global functions which I usually include in my projects are GCD helpers.

func delay(delay:Double, closure: dispatch_block_t) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}


func backgroundTask(closure: dispatch_block_t) {
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), closure)
}

func mainThreadTask(closure: dispatch_block_t) {
    dispatch_async(dispatch_get_main_queue(), closure)
}

These function don't need any information about a class so it makes sense to make them global instead of wrapping them inside a class.

Regarding instance methods, as answered by @Duncan C, they are called on instances, when you want to maintain a state. Below example shows usage of both static and instance methods.

enum TapType {
    case water
    case beer
}

struct Tap {
    let tapType: TapType

    //static method
    static func unlimitedBeer() -> Tap {
        let beer = Tap(tapType: .beer)
        beer.turnOn(forDuration: Float.greatestFiniteMagnitude)
        return beer
    }

    //instance method: will do operation on a particular instance of `Tap`.
    func turnOn(forDuration duration: Float) {
        //implementation
    }
}

let unlimitedBeer = Tap.unlimitedBeer()

You can always use convenience initializer to initialize an object with custom behaviour, but again, it's a matter of choice. In above example, I couldn't think of any convenience initializer which would give me unlimited beer.


I agree with (and upvoted) @Duncan C's answer, but just thought I'd throw in a couple of other pros/cons.

I tend to like global functions over class methods because global functions don't clutter up my classes. I like to keep my classes lean and thin. The global functions can be kept in a separate file that I can copy and paste, or import, into a given project as I need them. So I might have a file in my project called AnimateHelperFunctions that is just global functions related to that class. A given project may only need a couple of them, or most of them, or those plus a couple more that I discover I need. I can delete the ones I don't use in a given project from the file so as to keep that file neat and trim.

I just think that global functions are more modular and encourage me to factor out single tasks for a single function - a good global helper function that does exactly one thing and does it perfectly can also sometimes be abstracted or made generic and used in other contexts as well. You might have a project sometime where you realize you don't need the class - you just need its helper function, and there it is.

I would prefer a hundred simple global functions that I can pick and choose from over a giant bloated class.

You can accomplish much the same thing with extensions, of course, and to some degree it is a matter of taste, as there is very little difference (any?) between class methods and global functions except that to use the class method you have to drag along the entire class.

Unlike global state, there isn't any danger in a global function. Sure, anyone can call it, but the same is true of class methods, and the global function can only operate on the arguments you pass to it.

Tags:

Swift