local computed variables

Source: https://www.objc.io/blog/2018/04/10/local-vars/

Sometimes we want to compute the same expression twice in a function. For example, hereโ€™s a simplified version of some XML parsing code we recently wrote:

func parseElement(name: String, text: String) {
    if name == "trk" {
        let t = text.trimmingCharacters(in: .whitespaces)  // process element
    } else if name == "ele" {
        let elevationText = text.trimmingCharacters(in: .whitespaces)
        guard let elevation = Int(elevationText) else { return }
    } else {
        // no need to trim text
    }
}

Note that weโ€™ve duplicated thetext.trimmingCharacters(in: .whitespaces)in two branches. Of course, we can easily pull it out into a variable outside of theifstatement:

func parseElement(name: String, text: String) {    
    let trimmed = text.trimmingCharacters(in: .whitespaces)
    if name == "trk" {
        let t = trimmed
        // process element
    } else if name == "ele" {
        guard let elevation = Int(trimmed) else { return }
    } else {
        // no need to trim text
    }
}

This works fine, except that it can really slow down our parsing code: weโ€™re trimming the text for _every _element that we parse, but we only really need it in case oftrkandeleelements. Iftrimmedwere a struct or class property, we could writelazyin front of it, but alas, that doesnโ€™t work for a local variable.

Instead, we can maketrimmeda local computed property, like so:

func parseElement(name: String, text: String) {
    var trimmed: String { text.trimmingCharacters(in: .whitespaces) }
    if name == "trk" {
        let t = trimmed
        // process element
    } else if name == "ele" {
        guard let elevation = Int(trimmed) else { return }
    } else {
        // no need to trim text
    }
}

This will computetrimmedonly when you need it. Of course, this solution has its drawbacks, too: if you access thetrimmedcomputed property more than once, it also computes the value more than once. You have to be careful to only use it when you need it, and if you do need it multiple times, cache the value.

The technique above can also work well for complicated loop conditions. For example, hereโ€™s a completely made-up while loop with three conditions:

while !done && queue.count>1 && queue[1] != "stop" {
    // process the queue
}

If we want to group the two queue-related conditions into one, a local computed property can be used:

var canContinue: Bool { return queue.count > 1 && queue[1] != "stop" }

while !done && canContinue {
    // process the queue
}

For more advanced tricks with local properties, watchSwift Talk 61andSwift Talk 63, where we use Swift 4โ€™s KeyPaths to build a new hybrid type, combining useful features of both classes and structs โ€” a fun way to push the limits of the language!

Last updated